0

我正在尝试在拦截器中调用 API 服务。

我的 API 服务代码:

showAcknowledgement() {
    this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => {
      data.data.filter((res: any) => {
      if (data.isSuccessful === true) {
        this.noticeText = res.noticeText
        }
      } else if (data.isSuccessful === false) {
        this.message = [{ field: "", message: data.message[0].message }];
        this.objError = this.message;
        this.showError = true;
        window.scrollTo({ top: 0, behavior: 'smooth' });
      }
    })
    }, (error) => {
      this.message = [{ field: "", message: "error" }];
      this.objError = this.message;
      this.showError = true;
      window.scrollTo({ top: 0, behavior: 'smooth' });
    });
  }

我的拦截器代码:

  public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
        tap((resp) => {
            if (resp instanceof HttpResponse && resp.body.isNotificationAvailable) {
              // I need to call my service API
            }
        })
    );
  }

如果我在 Interceptor 内调用 API,它会多次命中。

4

0 回答 0