在我的Angular应用中,我可以用什么替换TAP方法?

发布于 2025-02-05 15:43:21 字数 590 浏览 4 评论 0原文

我有以下代码作为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;
                }
            ),
        );
}

它使用自Angular 8以来已弃用的TAP(第4行)。如何替换TAP?我完成的搜索没有帮助我找到答案。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杆小烟枪 2025-02-12 15:43:21

使用{Next,错误}对象

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

Use { next, error } object

   tap({
       next: (event) => {
           if (event instanceof HttpResponse) {
               this.spinnerService.requestEnded();
           }
       },
       error: (error: HttpErrorResponse) => {
           this.spinnerService.resetSpinner();
           throw error;
       }
   })
独孤求败 2025-02-12 15:43:21

我认为Tap没有弃用,只是使用TAP的方法已更改。
您可以尝试以下操作:

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

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'))
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文