【发布时间】:2020-02-05 07:19:17
【问题描述】:
我需要在通话过程中unsubscribe,但是当我这样做时没有HttpResponse。这对我来说是个问题,因为我还使用http 拦截器来捕捉何时应该显示加载图标。
所以在我的特定组件中,我有这个:
if (this.testSubscription)
this.testSubscription.unsubscribe(); // Stop original http request when calling getTestDetails with other parameters
this.testSubscription = this.getTestDetails(param1, param2);
还有我的拦截器:
intercept(request: HttpRequest<any>, next: HttpHandler) {
this.totalRequests++;
console.log(' intercept totalRequests: ', this.totalRequests);
this.loadingService.isLoading.next(true);
return next.handle(request).pipe(
tap(res => {
if (res instanceof HttpResponse) {
// I need to get here even when unsubscribing !!!!
this.decreaseRequests();
}
}),
catchError(err => {
this.decreaseRequests();
throw err;
})
);
}
所以当我在订阅上unsubscribe 时,我不确定如何触发我的拦截方法来捕捉它。
任何想法表示赞赏!
【问题讨论】:
-
您可以使用
finalize,它在您退订时也会触发,但在这种情况下,您将无法访问任何next,以防它成功通过。 -
一般情况下,您可以使用
switchMap平滑地取消订阅一个流并订阅另一个流。 -
Martin 的评论很容易解决,感谢大家的帮助!
标签: angular rxjs httprequest