我有一个返回表数据的请求,需要像 promise 等待数据加载一样处理该请求。为了将数据加载到表中,我必须使用异步/等待,但这会弄乱所有其他函数/方法。
如何在 ngOnInit() 上不使用 async/wait 的情况下将数据存储在 currentList 中?或者还有其他方法吗?
async getEducationList(): Promise<any> {
return this.educationList = await this.applicationClient.getEducations()
.toPromise()
.then((res) => {
this.educationListAll = res;
});
}
async ngOnInit() {
await this.getEducationList();
this.currentList = this.educationListAll;
}
注意 - this.applicationClient.getEducations() 是一个 Observable
最佳答案
试试这个方法
async ngOnInit() : Promise<void> {
this.currentList = await this.applicationClient.getEducations().toPromise();
this.initTable(data);
}
initTable(data) {
// Some code to handle the data
}
关于angular - 在 ngOnInit Angular 之前处理异步 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925816/