异步函数始终返回可观察的
下面的Anync函数是为通过后端创建用户列表
的。但是,当这种称为时,等待
in ngoninit
it(用户列表)返回作为可观察的可观察到。这里缺少的部分是什么?
async getUsers(): Promise<void> {
if (this.empId) {
this.userList = await this.empService.getEmpUsers(this.empId);
console.log(this.userList); // return Observable {}
}
}
This below async function was created to populate the userList
through the backend. But when this is called like this await this.getUsers();
in ngOnInit
it (userList) returns as an Observable. What is the missing part here?
async getUsers(): Promise<void> {
if (this.empId) {
this.userList = await this.empService.getEmpUsers(this.empId);
console.log(this.userList); // return Observable {}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Angular的HttpClient 总是返回可观察的。您需要使用
rxjs 6
.topromise()
或rxjs 7
:Angular's HttpClient always returns an Observable. You need to turn it into a promise by using
RxJS 6
.toPromise()
orRxJS 7
: