Typescript 角度HTTP拦截器-重定向到登录页面并跳过进一步的代码执行

Typescript 角度HTTP拦截器-重定向到登录页面并跳过进一步的代码执行,typescript,authentication,jwt,angular6,angular-http-interceptors,Typescript,Authentication,Jwt,Angular6,Angular Http Interceptors,我已经开发了Angular 6应用程序。 使用Web API实现JWT令牌身份验证。还实现了HttpInterceptor来监视请求并在每个请求中发送令牌 @Injectable() 导出类HttpRequestInterceptor实现HttpInterceptor{ 构造函数(专用路由器:路由器){} 截取(req:HttpRequest,next:HttpHandler):可观察{ var token=localStorage.getItem(“bearerToken”); 如果(令牌)

我已经开发了Angular 6应用程序。 使用Web API实现JWT令牌身份验证。还实现了HttpInterceptor来监视请求并在每个请求中发送令牌

@Injectable()
导出类HttpRequestInterceptor实现HttpInterceptor{
构造函数(专用路由器:路由器){}
截取(req:HttpRequest,next:HttpHandler):可观察{
var token=localStorage.getItem(“bearerToken”);
如果(令牌){
const newReq=req.clone(
{
headers:req.headers.set('Authorization'、'Bearer'+令牌)
});
返回next.handle(newReq)
.做(
(事件:HttpEvent)=>{
if(HttpResponse的事件实例){
返回事件;
}
else if(HttpErrorResponse的事件实例){
控制台日志(“事件”);
}
},
(错误)=>{
if(HttpErrorResponse的错误实例){
如果(err.status==401){
返回此.router.navigate(['/logout']);
}
}
}
);
}
否则{
返回next.handle(req);
}
}
};
this.http.post(url,JSON.stringify(this.selectedStatus),httpOptions)
.订阅(响应=>{
this.view=响应;
//this.fetchConfig();
this.showLoader=false;
this.showGrid=true;
},
(错误:任意)=>{
this.showLoader=false;
this.showGrid=true;
if(this.modalRef==null){
this.modalRef=this.modalService.open(ModalMessageboxComponent);
this.modalRef.componentInstance.message=“访问请求时出错”;
this.modalRef.componentInstance.messageBoxTitle=“错误”;
this.modalRef.result.then((result)=>{
this.modalRef=null;
}
)
}
}
)