我目前正在通过使用HTTPClient学习Angular 6和RXJS。
文档https://angular.io/guide/http指出,可以将内捕获错误this.http.get的使用方法pipe,然后tap像下面从官方文档举起例子。
getTextFile(filename: string) {
// The Observable returned by get() is of type Observable<string>
// because a text response was specified.
// There's no need to pass a <string> type parameter to get().
return this.http.get(filename, {responseType: 'text'})
.pipe(
tap( // Log the result or error
data => this.log(filename, data),
error => this.logError(filename, error)
)
);
}
Run Code Online (Sandbox Code Playgroud)
我试图像这样在我的方法中复制它
getLeads() : Observable<Lead[]> {
return this.http.get<Lead[]>('http://localhost:3000/leads').pipe(
tap (
error => console.log('error')
)
); …Run Code Online (Sandbox Code Playgroud)