angular - 拦截器中的 catchError 消除了取消请求的能力

标签 angular

我在某些组件中有请求,例如通过点击:

performRequest() {
    const request = this.http
      .get('https://randomfox.ca/floof/')
      .subscribe();

      request.unsubscribe();
}

我有 http 拦截器:

@Injectable()
export class MyInterceptor implements HttpInterceptor {
    public intercept(req: HttpRequest<any>, next: HttpHandler) {
        return next.handle(req); // all is ok
        return next.handle(req).pipe(catchError(e => throwError(e))); // request can't be cancelled now
    }
}

如果第一个 return 有效 - 请求被 unsubscribe() 取消但第二个 - 它没有被取消(我说的是请求级别的取消,比如请求不是挂起而是已取消)。

为什么会这样以及如何处理拦截器中的错误并具有取消请求的能力?

最佳答案

我找到了一些解决方法,所以它可能会有所帮助 - return next.handle(req).pipe(tap(noop, (err: any) =>/* 以某种方式处理*/));

另外,想提一下,我已经在单独的(和 +- 干净的)angular 项目中尝试过,但问题没有重现

关于angular - 拦截器中的 catchError 消除了取消请求的能力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54443430/

相关文章:

node.js - Angular 和NPM问题

css - 以 % 设置大小时不显示背景图像

javascript - 从表格行动态删除按钮

html - Aurelia:完整性检查模板 html?

Angular - 使用命名导出时出现错误 : "Cannot match any routes" while navigate from canActivate guard

angular - 如何从 Angular 6 自动生成的 XLIFF 文件中删除位置、源文件和行号?

angular - native 脚本异常 : Failed to find module: "crypto"

angular - 允许在 cdkDropList 中水平定位

javascript - 如何将 AngularJS 2 连接到 REST-API?

Firefox 上的 Angular 6 [WDS] 断开连接错误