我在某些组件中有请求,例如通过点击:
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/