How to cancel current request in interceptor - Angular 4

后端 未结 7 1943
情书的邮戳
情书的邮戳 2021-02-05 01:29

As you know it\'s possible to use Interceptors in new versions of Angular 4.

In mine, I want to cancel a request in interceptor in some conditions. So i

7条回答
  •  鱼传尺愫
    2021-02-05 02:05

    This is just a slight variant of RVP's answer

    import { NEVER } from 'rxjs';
    
    intercept(request: HttpRequest, next: HttpHandler): Observable> {
      if (stopThisRequest) {
        return NEVER;
      }
    
      return next.handle(request);
    }
    

    I used NEVER instead of EMPTY to avoid dealing with undefined values in my subscriptions (or promises).

    Use NEVER if you don't want your subscription callback to be invoked

提交回复
热议问题