如何在 ngZone 之外 运行 异步函数?

How to run async function outside ngZone?

我尝试在当前区域外运行脚本:

  async ngAfterViewInit(): Promise<void> {
     this.ngZone.runOutsideAngular(() => {
       await this.run();
     });
  }
  
  async run() { // TODO }

我收到这个错误:

'await' expressions are only allowed within async functions and at the top levels of modules.ts

ngAfterViewInit 函数是异步的,但您使用了 this.ngZone.runOutsideAngular 和非异步回调函数。

您的代码需要如下所示...

ngAfterViewInit(): void {
    this.ngZone.runOutsideAngular(async () => {
        await this.run();
    });
}

async run() { // TODO }