最近在页面跳转的时候,出现如下提示:
Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?
出现地方:在点击退出登录提示框,跳转到登录页面的时候会出现这句提示。
导致的问题:登录成功后,跳转到主页面,页面不走ngOnInit(),然后切换tab页的时候,才走这个方法,导致页面空白。
出现概率:每三次退出登录会出现一次这种情况。
解决方法:
import { Component, OnInit,NgZone } from '@angular/core';
import { Router } from '@angular/router';
constructor(private ngZone:NgZone,private router:Router) {}
async onClickLogout() {
const alert = await this.alertController.create({
header: this.translateText.login_out_title,
message: this.translateText.login_out_subTitle,
buttons: [
{
text: this.translateText.sureBtn,
handler: (blah) => {
////成功清除必要信息 并展示登录页面
this.logoutSuccess();
}
}, {
text: this.translateText.cancelBtn,
handler: () => {
}
}
]
});
await alert.present();
}
logoutSuccess() {
setTimeout(() => {
this.ngZone.run(() => this.router.navigateByUrl('/login')).then();//这里是关键地方
}, 10);
}
this.ngZone.run(() => this.router.navigateByUrl('/login')).then();这句话是关键的地方;
出现这个警告的原因不清楚。
猜测:可能和弹出框的异步有关,弹出框可能截取了路由跳转的