路由函数是在服务中调用的。我收到这样的警告:
Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?但是,我不能调用this.ngZone.run(...) ,因为路由完成时需要事件。默认情况下,ngZone.run() => void不返回任何值。
该函数如下所示:
private handleRedirectRouting(): Observable<boolean> {
return this.route.queryParamMap.pipe(
switchMap((params) => {
if (params.get('_redirect')) {
const route = params.get('_redirect');
return from(this.router.navigate([route]));
} else if (this.router.url.includes('auth/login')) {
const route = '/';
return from(this.router.navigate([route]));
}
return of(true);
})
);
}我该怎么解决呢?
发布于 2022-10-24 07:46:12
ngZone.run()是正确的,因为它可以回报一个承诺。
from(this.ngZone.run(() => {
return this.router.navigate([route]));
}))发布于 2022-10-24 07:45:33
我可以通过显式传递函数调用的返回类型来解决代码错误:
this.ngZone.run<Observable<boolean>>(() => {
return from(this.router.navigate([route]));
})https://stackoverflow.com/questions/74177794
复制相似问题