【发布时间】:2018-10-14 18:31:28
【问题描述】:
在我的 Angular 应用程序中,我看到 chrome(取消)api 调用启动得太快了。如果请求未完成,我还有一个 HttpInterceptor 会在 500 毫秒后触发每个 HttpClient 请求的加载指示器。但是,在获得(取消)的请求上,似乎没有任何新事件可以随后隐藏我的加载指示器。
有没有办法检测 HttpInterceptor 中的“已取消”请求,以便我可以再次隐藏加载指示器?
export class GlobalHttpInterceptor implements HttpInterceptor {
constructor(
private sessionService: SessionService,
private loadingService: LoadingService
) { }
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
setTimeout(() => {
if (showLoading) {
this.loadingService.show();
}
}, 500);
let showLoading = true;
if (this.sessionService.headers.length > 0) {
for (const x of this.sessionService.headers) {
req = req.clone({
headers: req.headers.set(x.key, x.value)
});
}
}
return next.handle(req)
.do(
(response) => {
if (response instanceof HttpResponse) {
showLoading = false;
this.loadingService.hide();
}
},
(error) => {
showLoading = false;
this.loadingService.hide();
}
);
}
}
【问题讨论】:
-
你找到办法了吗?
-
将处理程序响应包装在另一个 Observable 中,如 here 所述,似乎工作得很好
标签: angular httpclient angular-http-interceptors