首页 > 解决方案 > 如何通过在拦截器中调用 API 服务方法来避免无限次 API 命中

问题描述

我有一个带有 API 服务方法的组件

TS

     showAcknowledgement() {
this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => {
              data.data.filter((res: any) => {
              if (data.isSuccessful === true) {
                this.noticeText = res.noticeText
                this.noticeAttachment = res.noticeAttachement,
                this.noticeCode = res.noticeCode
              }
            })
            });
          }

我需要将此服务方法调用到 Interceptor:

public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
      tap((resp) => {
        if (resp instanceof HttpResponse && resp.body.isNotificationAvailable) {
          this.showAcknowledgement();
        }
      })
    );
  }

API 达到无限次在此处输入图像描述

标签: angulartypescriptapiinterceptorangular-http-interceptors

解决方案


推荐阅读