如何在 http 拦截器中处理取消的请求?我尝试过SO的各种方法,但似乎没有一个能捕获它。
这就是我的拦截器的样子,
public intercept(req: HttpRequest<any>, next: HttpHandler) {
const xyz = this._cookieService.get('xyz');
const abc = this._cookieService.get('abc');
let authReq;
if (jwt === "undefined" || jwt === undefined) {
authReq = req.clone({ headers: req.headers.set('Authorization', xyz) });
} else {
authReq = req.clone({setHeaders: { Authorization: 'bearer ' + abc } });
}
return next
.handle(authReq)
.pipe(
catchError((err: HttpErrorResponse) => this.catchErrorHandler(err))
);
}
我也尝试过do
、finalize
方法。就是这样,我的授权过期了,之后的所有请求就一一崩溃了,分页了。我想用代码来处理它,这样页面就不会中断。
我尝试过的方法:
https://stackoverflow.com/a/50620910/6630504
https://stackoverflow.com/a/55756885/6630504
最佳答案
有一个名为 finalize 的 RxJS 运算符.
Returns an Observable that mirrors the source Observable, but will call a specified function when the source terminates on complete or error. The specified function will also be called when the subscriber explicitly unsubscribes.
return next
.handle(authReq)
.pipe(
catchError((err: HttpErrorResponse) => this.catchErrorHandler(err)),
finalize(() => console.log('finalize'))
);
关于javascript - 如何在 http 拦截器 Angular 中处理/检测取消的请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57763719/