如何使用 Angular 5 识别来自 HTTP 拦截器的特定请求?

Cha*_*dan 8 interceptor observable angular

我在 Angular 5 中使用 HTTPInterceptor 功能。它在克隆 http 请求并发送到服务器(后端服务器)时按预期工作。我只从 HTTPInterceptor 显示和隐藏应用程序加载器,这也工作正常,但我对一个 GET 请求使用了轮询,该请求每 5 秒从后端服务器获取数据,这让用户感到恼火。那么,有没有办法检查 HTTPInterceptor 中的特定请求?并且也不允许根据该请求显示/隐藏加载程序。

以下是拦截函数的当前代码片段:

  intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.loadingIndicatorService.showLoader();
    this.customAuthorizationHeader();
    const apiRequest = req.clone({headers:this.headers});
    return next.handle(apiRequest).do
    ((response) => {
        if (response instanceof HttpResponse) {
          this.loadingIndicatorService.hideLoader();
        }
      },
      (error) => {
        this.loadingIndicatorService.hideLoader();
      });
  };
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Env*_*vil 6

您可以检查是否req.url等于要排除的路径,如下所示:

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // put this before your current code
    if (req.url.indexOf('/* the api path you want to exclude*/') === -1) {
      return next.handle(req);
    }
    // do your stuff here.
    return next.handle(req);
  }
Run Code Online (Sandbox Code Playgroud)