使用angular的toPromise()将异步方法转化为同步方法
b.service.ts
// 获取任务分类
public getRunningCategory(): Observable<any> {
const apiUrl = `/api/runtime/task/v1.0/categories`
return this.http.get(apiUrl)
}
A.ts文件:
getHero(): Promise<any> {
//this.service.getRunningCategory() 封装的接口文件
return this.service.getRunningCategory().toPromise();
}
// 存在异步方法,前面加async
async openTaskForm(data: any) {
// 添加await表示等待结果返回
await this.getHero().then(dataCategory => {
//在这里面编写逻辑,处理加工后的数据
})
await this.getHero2().then(dataCategory => {
//在这里面编写逻辑,处理加工后的数据
})
}