Angular Tap 已弃用

Beh*_*hih 6 javascript rxjs typescript angular-material angular

我的角度应用程序中有以下 http 拦截器:

    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();
                    }));
        }
    }
Run Code Online (Sandbox Code Playgroud)

在我用过的行中

轻敲

我收到这个警告:

不要传递单独的回调参数,而是使用观察者参数。采用单独回调参数的签名将在 V8 中删除

代码正在运行,但我在上面的警告旁边看到了“tap”关键字的罢工

Nen*_*vic 8

而不是这个:

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

做这个:

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

Run Code Online (Sandbox Code Playgroud)

  • 在 Tap 中添加 {} 解决了问题。谢谢 (2认同)