angular - 在 ngOnInit Angular 之前处理异步 Promise

标签 angular asynchronous promise async-await es6-promise

我有一个返回表数据的请求,需要像 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/

相关文章:

javascript - Angular 2 和 LeafLet Uncaught ReferenceError : <function> is not defined at HTMLButtonElement. onclick

angular - 我应该将什么样的对象传递给 `FormBuilder.group()` 才能正确实例化字段?

JavaScript:简单的异步问题

node.js - 如何在一个 get 请求中返回多个 Mongoose 集合?

javascript - 为什么 Promise 记录数据但返回相同数据的未定义

javascript - Angular 2 Pipe 参数失败

angular - 在新文件夹中运行 npm install 后 ng build --prod build 失败

php - 在 PHP 中进行异步处理的最佳方式

node.js - Node/Express 应用程序中延迟超过一小时的触发功能

javascript - 嵌套 Promise 链是否被视为反模式?