如何在Angular中拦截已取消的http请求?
Angular取消http请求的速度非常快,我想截获那些被取消的请求。是否可以在拦截器中捕获已取消的请求? 下面是我的拦截器代码片段,我想在这里捕获已取消的请求如何在Angular中拦截已取消的http请求?,angular,http,angular-http-interceptors,Angular,Http,Angular Http Interceptors,Angular取消http请求的速度非常快,我想截获那些被取消的请求。是否可以在拦截器中捕获已取消的请求? 下面是我的拦截器代码片段,我想在这里捕获已取消的请求 intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { this.onStartRequest(); // Pass the cloned request instead
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
this.onStartRequest();
// Pass the cloned request instead of the original request to the next handle
return next.handle(req).do(
(event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do something
}
},
(err: any) => {
if (err instanceof HttpErrorResponse) {
// do something
}
}
);
}
在使用下一个处理程序时,必须使用catchError RxJs运算符,代码如下:
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
this.onStartRequest();
// Pass the cloned request instead of the original request to the next handle
return next.handle(req).do(
(event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do something
}
})
.catchError((err: HttpErrorResponse) => {
// Handle errors
});
}
PS:别忘了导入catchError操作符。您是否尝试过finalize事件
return next.handle(req).pipe(
finalize(() => {
// request completes, errors, or is cancelled
})
);
请解释您在此上下文中所说的“取消的请求”是什么意思。可能与我如何确定在finalize中取消的请求重复?它是否有任何特定的密钥显示特定请求已被取消?