/* 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)