异步函数始终返回可观察的

发布于 2025-02-13 20:08:26 字数 381 浏览 1 评论 0原文

下面的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2025-02-20 20:08:27

Angular的HttpClient 总是返回可观察的。您需要使用rxjs 6 .topromise()rxjs 7

async getUsers(): Promise<void> {
        if (this.empId) {
            this.userList = await firstValueFrom(this.empService.getEmpUsers(this.empId))
        }
    }

Angular's HttpClient always returns an Observable. You need to turn it into a promise by using RxJS 6 .toPromise() or RxJS 7:

async getUsers(): Promise<void> {
        if (this.empId) {
            this.userList = await firstValueFrom(this.empService.getEmpUsers(this.empId))
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文