【发布时间】:2018-10-13 22:45:08
【问题描述】:
/* error handler that will be used below in pipe with catchError()
* when resource fetched with HttpClient get() */
private _handleError<T> (operation: string, result?:T) {
return( error: any): Observable<T> => {
console.error( operation + ' ' + error.message );
// or something else I want to do
return of(result as T); // lets me return innocuous results
}
}
getObjects() {
return this.http.get<any[]>(this.myUrl).pipe(
catchError(this._handleError('my error', [])
);
}
现在使用tap 处理错误
getObjects() {
return this.http.get<any[]>(this.myUrl).pipe(
tap( objects => {
// whatever action like logging a message for instance
}, err => {
console.error(err);
// whatever else I want to do
})
);
}
为什么我应该选择一种方法而不是另一种?使用tap() 处理 HTTP 错误是否会在发生时让我的应用继续运行?
【问题讨论】:
-
tap只是为了产生副作用,它根本不修改链,所以处理错误使用catchError。