Home > OS >  What can I replace the tap method with in my Angular app?
What can I replace the tap method with in my Angular app?

Time:06-08

I have the following code as part of an http-interceptor:

handler(next, request) {
    return next.handler(request)
        .pipe(
            tap(
                (event) => {
                    if (event instanceof HttpResponse) {
                        this.spinnerService.requestEnded();
                    }
                },
                (error: HttpErrorResponse) => {
                    this.spinnerService.resetSpinner();
                    throw error;
                }
            ),
        );
}

It uses tap (line 4) which has been deprecated since Angular 8. How can I replace tap? The searched I've done have not helped me find an answer. Thanks.

CodePudding user response:

Use { next, error } object

   tap({
       next: (event) => {
           if (event instanceof HttpResponse) {
               this.spinnerService.requestEnded();
           }
       },
       error: (error: HttpErrorResponse) => {
           this.spinnerService.resetSpinner();
           throw error;
       }
   })

CodePudding user response:

I think tap is not deprecated, just the way of using tap is changed. You can try the following:

    updateHero(hero: Hero): Observable<any> {
  return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe(
    tap(_ => this.log(`updated hero id=${hero.id}`)),
    catchError(this.handleError<any>('updateHero'))
  );
}
  • Related