Home > Mobile >  Angular Tap is deprecated
Angular Tap is deprecated

Time:12-13

I have following http interceptor in my angular app:

    import { Observable } from 'rxjs';
    import { Injectable } from '@angular/core';
    import { HttpInterceptor, HttpResponse } from '@angular/common/http';
    import { HttpRequest } from '@angular/common/http';
    import { HttpHandler } from '@angular/common/http';
    import { HttpEvent } from '@angular/common/http';
    import { tap } from 'rxjs/operators';
    import { SpinnerService } from '../sharedServices/spinner.service';
    
    @Injectable()
    export class CustomHttpInterceptor implements HttpInterceptor {
    
        constructor(private spinnerService: SpinnerService) { }
    
        intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    
    
            this.spinnerService.show();
            
            return next.handle(req)
                 .pipe(tap((event: HttpEvent<any>) => {
                        if (event instanceof HttpResponse) {
                            this.spinnerService.hide();
                        }
                    }, (error) => {
                        this.spinnerService.hide();
                    }));
        }
    }

in the line I used

tap

I get this warning:

Instead of passing separate callback arguments, use an observer argument. Signatures taking separate callback arguments will be removed in V8

the code is working but I see a strike on "tap" keyword alongside above warning

CodePudding user response:

Instead of this:

tap(
  (event: HttpEvent<any>) => {
    if (event instanceof HttpResponse) {
      this.spinnerService.hide();
    }
  }, 
  (error) => {
    this.spinnerService.hide();
  }
)

Do this:

tap({
  next: (event: HttpEvent<any>) => {
    if (event instanceof HttpResponse) {
      this.spinnerService.hide();
    }
  }, 
  error: (error) => {
    this.spinnerService.hide();
  }
})

  • Related