【发布时间】:2019-07-08 08:49:19
【问题描述】:
我正在尝试在ngOnInit 上获取async 函数的结果。
在 .ts 文件中,我添加了这个函数:
async load()
{
this.picaes = await this.userService.getAffectedExpertisesPromiseSE(25);
console.log("-Inside load--------------" + this.picaes);
return this.picaes;
}
在 ngOnInit 上,我称之为 async 函数:
console.log("-Outside load--------------" + this.load());
在 service.user.ts 文件中,我进行了以下调用:
async getAffectedExpertisesPromiseSE(id: number): Promise<any>
{
var url= `${this.baseUrl}/users/expertisesObj/${id}`;
var result= await (await fetch(url)).text();
return result;
}
产生:
-外部加载--------------[object Promise]
-内部加载--------------[{"id":1,"name":"Angular","selected":true},{"id":2, "name":"SpringBoot","selected":true},{"id":3,"name":"Primefaces","selected":true}]
所以我的问题是我无法在控制台外部加载上得到结果。
你能告诉我我错过了什么吗? 非常感谢。
【问题讨论】:
-
const v = await this.load(); console.log(v); -
您好@Reactgular 先生,感谢您的回复。我得到 'await' 表达式只允许在 sublime 上的异步函数中使用。