小编yac*_*uat的帖子

选择哪个 RxJS 操作符来处理 HTTP 错误:tap 还是 catchError?

/* 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', [])
  );
}
Run Code Online (Sandbox Code Playgroud)

现在tap用于处理错误

getObjects() {
  return this.http.get<any[]>(this.myUrl).pipe(
    tap( objects => { …
Run Code Online (Sandbox Code Playgroud)

rxjs angular-httpclient

13
推荐指数
1
解决办法
6432
查看次数

标签 统计

angular-httpclient ×1

rxjs ×1