要知道请求是否已完成,我会做if (httpEvent instanceof HttpResponse)
。同样,如何知道HttpInterceptor
中是否取消了请求?下面是我的intercept
功能。
intercept(httpRequest: HttpRequest<any>, httpHandler: HttpHandler): Observable<HttpEvent<any>> {
this.requestsPending++;
if (this.typeAheads.includes(httpRequest.url.split('/').pop()))
this.slimLoadingBarService.start();
else
this.flsLoaderService.start();
return httpHandler.handle(httpRequest)
.do((httpEvent: HttpEvent<any>) => {
if (httpEvent instanceof HttpResponse) {
// decrementing counter on each request return
this.requestsPending--;
if (this.typeAheads.includes(httpEvent.url.split('/').pop())) {
if (this.requestsPending === 0)
this.slimLoadingBarService.complete();
}
else {
if (this.requestsPending === 0)
this.flsLoaderService.stop();
}
}
}, () => {
this.slimLoadingBarService.complete();
// reset counter on error
this.requestsPending = 0;
this.flsLoaderService.stop();
});
}
我也有这个问题,这是我最近来的一个解决方案:
要知道请求是否被取消,您可以使用finally
运算符,例如:
let cancelled = true;
return next.handle(request).do(
undefined,
() => {
// error
cancelled = false;
},
() => {
// completed
cancelled = false;
},
).finally(
() => {
if (cancelled) {
// do things
}
},
);
最后我找到了解决方案
intercept(httpRequest: HttpRequest<any>, httpHandler: HttpHandler):
Observable<HttpEvent<any>> {
this.requestsPending++;
//...
return new Observable(observer => {
let sub = httpHandler.handle(httpRequest)
.pipe(tap(event => {
//... your logic
}))
.subscribe(event => observer.next(event));
return () => {
if (!sub.closed) {
this.requestsPending--;
sub.unsubscribe();
}
};
});
}
为了使它工作,我使用了上面两种技术的混合,因为他们没有为我开箱即用。这是我的工作代码:
// your increment logic here
return new Observable(observer => {
let isCanceled = true; << here
const sub = next.handle(req)
.pipe(
tap(
(rsp: HttpResponse<any>) => {
if (rsp.type === HttpEventType.Response) {
isCanceled = false;
// your decrement logic here
}
},
(rspError: HttpErrorResponse) => {
isCanceled = false;
// your decrement logic here
throwError(rspError); // re-throw same e
},
),
)
.subscribe(observer); // << here
return () => {
if (isCanceled) {
// your decrement logic here
sub.unsubscribe();
}
};
});
似乎angular忽略了中止事件。以下是Angular来源:https://github.com/angular/angular/blob/5.0.1/packages/common/http/src/xhr.ts
如果请求被中止,则返回的Observable会通知订阅者。