採用了棱角分明4.3角4.3 - HTTP 401狀態 - 。Router.Navigate([「/登錄」]沒有導航
我堅持與401錯誤和router.Navigate一個很奇怪的問題,我處理catch中的異常和檢查錯誤狀態如果status是401,那麼導航到登錄頁面在這個狀態下,router.navigate不起作用,只有當status是401 [控制檯沒有錯誤]時它才工作如果status是404然後this.router.navigate(以未找到路由組件]工作正常,這是我的代碼 -
protected get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return this.http.get(url, options)
.catch((response) => {
if (response.status === 401) {
console.log('401 error - navigating');
this.router.navigate(['/login']);
return Observable.of(response);
}
return Observable.throw(response);
});
};
如果錯誤代碼是404,那麼router.Navigate的工作沒有任何問題。請參見下面的代碼[在此代碼router.navigate工作正常,並導航到NOTFOUND頁] -
protected get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return this.http.get(url, options)
.catch((response) => {
if (response.status === 404) {
console.log('404 error - navigating');
this.router.navigate(['/notfoundpage']);
return Observable.of(response);
}
return Observable.throw(response);
});
};
你檢查response.status。你確定它是401嗎? –
是的,控制檯顯示相應的日誌消息 - '401錯誤.....'。它打印控制檯消息,即狀態是401 – user2216584