Rxjs 如何在angular5中取消Http侦听器中的请求?
我的angular应用程序有一个名为“listdata.component.ts”的组件。在该组件的ngOnInit()中,有两个api调用。但当这2个请求得到401响应时,注销api调用发生了2次。我的拦截代码Rxjs 如何在angular5中取消Http侦听器中的请求?,rxjs,angular5,angular-http-interceptors,request-cancelling,Rxjs,Angular5,Angular Http Interceptors,Request Cancelling,我的angular应用程序有一个名为“listdata.component.ts”的组件。在该组件的ngOnInit()中,有两个api调用。但当这2个请求得到401响应时,注销api调用发生了2次。我的拦截代码 @Injectable() export class TokenInterceptor implements HttpInterceptor { notificationItem: any; constructor(public authService: AuthGua
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
notificationItem: any;
constructor(public authService: AuthGuardService, private router: Router, private notification: NotificationService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (this.authService.tokenValid()) {
const authReq = request.clone({ headers: request.headers.set("Authorization", 'Bearer ' + this.authService.getToken()) });
return next.handle(authReq).do((event: HttpEvent<any>) => {
}, err => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
if(authReq.url.indexOf('/logout') > -1){
localStorage.clear();
this.router.navigate(['login']);
}
else
{
this.authService.logout()
}
}
}
});
}
else{
return next.handle(request);
}
}
}
@Injectable()
导出类TokenInterceptor实现HttpInterceptor{
通知事项:任何;
构造函数(公共authService:AuthGuardService,专用路由器:路由器,专用通知:NotificationService){}
拦截(请求:HttpRequest,下一步:HttpHandler):可观察{
if(this.authService.tokenValid()){
const authReq=request.clone({headers:request.headers.set(“Authorization”、'Bearer'+this.authService.getToken())});
返回next.handle(authReq).do((事件:HttpEvent)=>{
},err=>{
if(HttpErrorResponse的错误实例){
如果(错误状态===401){
if(authReq.url.indexOf('/logout')>-1){
localStorage.clear();
this.router.navigate(['login']);
}
其他的
{
this.authService.logout()
}
}
}
});
}
否则{
下一步返回。处理(请求);
}
}
}
我想在一次401响应后取消所有401请求,并只调用注销一次。takeUntil()操作员是否可以解决我的问题?提前感谢