angular - 如何在 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
答案#1
ngAfterViewInit 函數是非同步的,但你卻使用了 this.ngZone.runOutsideAngular 並搭配非非同步的回呼函數。
你的程式碼需要看起來像這樣...
ngAfterViewInit(): void {
this.ngZone.runOutsideAngular(async () => {
await this.run();
});
}
async run() { // TODO }
