Ionic4--解决【Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?】
最近在页面跳转的时候,出现如下提示:
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();这句话是关键的地方;
出现这个警告的原因不清楚。
猜测:可能和弹出框的异步有关,弹出框可能截取了路由跳转的
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
