[英]How interceptor terminate handle Angular 4?
I have an Ionic 3 app project, I add an interceptor when the app receives a response from the server , when the response's status equals 401 then the app skip to the login page, I use app.getRootNav().setRoot(LoginPage)
.我有一个 Ionic 3 应用程序项目,当应用程序收到来自服务器的响应时,我添加了一个拦截器,当响应的状态等于 401 时,应用程序跳转到登录页面,我使用
app.getRootNav().setRoot(LoginPage)
。 However, when the page with a toast receives a 401 response, the toast will still appear, which is unexpected但是,当带有toast的页面收到401响应时,仍然会出现toast,这是意料之中的
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.rest = this.inj.get(RestProvider);
return next.handle(req).do(
res => {
...
},
err => {
if (err instanceof HttpErrorResponse) {
if (err.status == 401) {
this.app.getRootNav().setRoot(LoginPage);
super.showToast(this.toastCtrl, "sorry, due to authorization, you must login again")
return;
} else {
throw err;
}
}
})
}
then when my HTTP request like this:然后当我的 HTTP 请求是这样的:
this.http.post(this.baseUrl + "/recvUsername",param)
.subscribe(
res => {
this.initRecvUsernames(res["data"]);
},
err => {
console.error("get receive user error:" + err.message);
super.showToast(this.toast, "get receive user error");
}
)
then the toast at request page will show when I skip to the login page, how to terminate the toast at request page?那么当我跳转到登录页面时,toast at request 页面将显示,如何终止 toast at request 页面?
You can pass a flag in http params and remove it in interceptor您可以在 http params 中传递一个标志并在拦截器中将其删除
const params = req.params;
const isPreventToast = params.has('preventToast');
params.delete('preventToast')
const newReq = req.clone({ params });
return next.handle(newReq).do(..., err => { if(!isPreventToast) ... })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.