StackBlitz 中的错误:'Unexpected strict mode reserved word' 试图将 async/await 与 subscription.toPromise() 一起使用
Error in StackBlitz: 'Unexpected strict mode reserved word' trying to use async/await with subscription.toPromise()
我在 StackBlitz here.
上设置了 Angular webapp 的一部分
在其中,我正在试验实现 mat-table 的最佳方法,其中数据来自 firebase,具有过滤、分页和排序功能。
最后写了一个 class 来实现调用的 MatTableDataSource 接口,VideoDataSource
in './videoDataSource.model.ts'。其中,有一个方法叫做loadMatches()
。这是一个 async
方法:
async loadMatches(): Promise<any>{
console.log("loadMatches entered");
let results = await this.dbService.getMatchesV2().toPromise();
console.log("results in loadMatches");
console.log(results);
let dbMatches = results.map(Match.fromJson);
return dbMatches.toPromise();
}
其中 dbService.getMatchesV2()
又来自注入的服务,看起来像这样:
getMatchesV2(){
return this.db.list<Match>('/matches').valueChanges();
}
其中 db 是 AngularFireDatabase
.
类型数据库服务的参数
app.component.ts 中的 loadMatches()
调用如下所示:
this.data = await this.dataSource.loadMatches();
其中数据是 AppComponent
类型 Match[]
的参数。
我收到错误,'Unexpected strict mode reserved word',参考了没有任何意义的行号(在 app.module.ts 中;我想这是一个 post-compile电话号码?)。在我尝试实施 async/await 后,问题立即出现。 DatabaseService 主要处理 Observables,但我认为 .toPromise()
足以解决问题。关于发生了什么的任何见解?非常感谢!
发生了什么事?
保留关键字错误是由您的内部依赖项之一引发的。
具体在哪里?
在 app.component.ts
中,你有这一行 -
this.data = await this.dataSource.loadMatches();
修复了什么?
async ngOnInit() {
为什么?!
由于您使用的是 await
,因此您在其中使用 await 的函数需要标记为 async
。 await 表达式只允许在异步函数中使用。在您的例子中,它是 ngOnInit
。 (记得在其他地方也这样做)。您可以在 Typescript 1.7's release notes
中阅读更多内容
一般如何调试这些问题?
这是其中一个问题出在您实际寻找的地方。因此,对 linter 错误进行一般检查(所有 linter 都好!),并在不同的浏览器中尝试会有所帮助。您可能会在不同的浏览器中得到不同的错误和堆栈跟踪。
好的!好的!但是错误会消失吗?有用吗?
给你 - https://stackblitz.com/edit/angular-tmgbw7?file=src%2Fapp%2Fapp.component.ts
我在 StackBlitz here.
上设置了 Angular webapp 的一部分在其中,我正在试验实现 mat-table 的最佳方法,其中数据来自 firebase,具有过滤、分页和排序功能。
最后写了一个 class 来实现调用的 MatTableDataSource 接口,VideoDataSource
in './videoDataSource.model.ts'。其中,有一个方法叫做loadMatches()
。这是一个 async
方法:
async loadMatches(): Promise<any>{
console.log("loadMatches entered");
let results = await this.dbService.getMatchesV2().toPromise();
console.log("results in loadMatches");
console.log(results);
let dbMatches = results.map(Match.fromJson);
return dbMatches.toPromise();
}
其中 dbService.getMatchesV2()
又来自注入的服务,看起来像这样:
getMatchesV2(){
return this.db.list<Match>('/matches').valueChanges();
}
其中 db 是 AngularFireDatabase
.
app.component.ts 中的 loadMatches()
调用如下所示:
this.data = await this.dataSource.loadMatches();
其中数据是 AppComponent
类型 Match[]
的参数。
我收到错误,'Unexpected strict mode reserved word',参考了没有任何意义的行号(在 app.module.ts 中;我想这是一个 post-compile电话号码?)。在我尝试实施 async/await 后,问题立即出现。 DatabaseService 主要处理 Observables,但我认为 .toPromise()
足以解决问题。关于发生了什么的任何见解?非常感谢!
发生了什么事?
保留关键字错误是由您的内部依赖项之一引发的。
具体在哪里?
在 app.component.ts
中,你有这一行 -
this.data = await this.dataSource.loadMatches();
修复了什么?
async ngOnInit() {
为什么?!
由于您使用的是 await
,因此您在其中使用 await 的函数需要标记为 async
。 await 表达式只允许在异步函数中使用。在您的例子中,它是 ngOnInit
。 (记得在其他地方也这样做)。您可以在 Typescript 1.7's release notes
一般如何调试这些问题?
这是其中一个问题出在您实际寻找的地方。因此,对 linter 错误进行一般检查(所有 linter 都好!),并在不同的浏览器中尝试会有所帮助。您可能会在不同的浏览器中得到不同的错误和堆栈跟踪。
好的!好的!但是错误会消失吗?有用吗?
给你 - https://stackblitz.com/edit/angular-tmgbw7?file=src%2Fapp%2Fapp.component.ts