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
解决方案
ngAfterViewInit 函数是异步的,但是您使用 this.ngZone.runOutsideAngular 和非异步回调函数。
你的代码需要看起来像这样......
ngAfterViewInit(): void {
this.ngZone.runOutsideAngular(async () => {
await this.run();
});
}
async run() { // TODO }
推荐阅读
- reactjs - 组件状态中的 LocalStorage
- php - 如何从最小值获取关联的行数据
- docker - dind:注册表镜像被忽略
- docker - 允许一个带有 docker 的私有注册表
- node.js - wget() 到 /tmp/wget 产生“EMFILE,打开的文件太多”
- neo4j - WITH 语句的 Neo4j 语法
- multithreading - 'perlbrew list-modules' 确实返回 'Perl' 而没有别的
- powershell - 将 diskpart 与 PowerShell 一起使用
- javascript - 猫头鹰轮播“navText:”不添加箭头
- javascript - 如何在javascript中合并两个嵌套对象