简体   繁体   English

Angular Tap 已弃用

[英]Angular Tap is deprecated

I have following http interceptor in my angular app:我的 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();
                    }));
        }
    }

in the line I used在我使用的行中

tap轻敲

I get this warning:我收到此警告:

Instead of passing separate callback arguments, use an observer argument.不要传递单独的回调 arguments,而是使用观察者参数。 Signatures taking separate callback arguments will be removed in V8采用单独回调 arguments 的签名将在 V8 中删除

the code is working but I see a strike on "tap" keyword alongside above warning代码有效,但我在上面的警告旁边看到了“tap”关键字

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();
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM