最近在頁面跳轉的時候,出現如下提示:
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();這句話是關鍵的地方;
出現這個警告的原因不清楚。
猜測:可能和彈出框的異步有關,彈出框可能截取了路由跳轉的