问题描述
在闭包函数中跳转路由有时会出现路由无法跳转并带有 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([*])即可