如何在打字稿的构造函数中编写异步函数



我想知道,是否有办法在Typescript(Ionic 4:Angular(的构造函数中编写异步函数。我只想读出一个json文件,但在整个代码中需要它,并希望在开始时触发函数。这就是我想在构造函数中调用的内容。

async ReadFile(filename: string, fileFunktion: string) {
this.data = await this.http.get(filename).toPromise();
return this.data;
}

有没有办法在构造函数中调用它?

你可以使用你的异步函数作为承诺

class YourClass {
constructor() {
ReadFile(filename, fileFunction)
.then(data => /* do something */);
}
}