如何在拦截器中取消当前请求-Angular 4 [英] How to cancel current request in interceptor - Angular 4

查看:479
本文介绍了如何在拦截器中取消当前请求-Angular 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,可以在新版本的Angular 4中使用 Interceptors .

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.

推荐答案

我认为要切断拦截器链,您要做的就是简单地返回一个空的Observable,如下所示:

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);
}

这篇关于如何在拦截器中取消当前请求-Angular 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆