How to cancel current request in interceptor - Ang

2020-05-29 19:43发布

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 is it possible? or maybe what should I ask is, Which way I should do that?

Also It will be Ok! if I found a way to rewrite some response to the request instead of canceling it.

7条回答
疯言疯语
2楼-- · 2020-05-29 20:48

I think all you have to do to cut the interceptor chain is to simply return an empty Observable like so:

import { EMPTY } from 'rxjs';

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  if (stopThisRequest) {
    return EMPTY;
  }

  return next.handle(request);
}
查看更多
登录 后发表回答