angular 解决 Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'? 提示

问题描述

在闭包函数中跳转路由有时会出现路由无法跳转并带有 Navigation triggered outside Angular zone, did you forget to call ‘ngZone.run()’?的提示
闭包提示

解决方案
import { Component, OnInit, NgZone } from '@angular/core';
export class * {
	 constructor(
            private route: Router,
            private ngZone: NgZone
      ) {}
      
       public navigate(commands: any[]): void {
            this.ngZone.run(() => this.route.navigate(commands)).then();
      }
}

在闭包函数调用前声明that = this,使用that.navigate([*])即可

猜你喜欢

转载自blog.csdn.net/weixin_43909915/article/details/90289426