首页 > 解决方案 > 如何在Angular中将数据传递给订阅者

问题描述

我有一个订阅“用户”的以下服务。

 public getEmployees(): Observable<User> {
    const url = `${this.baseUrl}/employee`;
    return this.http.get<User>(url);
  }

我的问题是如何使用用户数据调用 getEmployees。我正在尝试使用以下代码,但出现错误?

const _user: User = {
      userId: "30294
}

 this.userAccountService.getEmployees().subscribe((userprofile: _user) => {
      console.log(userprofile.firstName);
    });

错误:_user 引用了值,但在这里使用了一个类型

标签: angularangular-servicessubscribe

解决方案


您正在_user用作类型userprofile: _user

像这样修复:

const _user: User = {
      userId: "30294
}
 this.userAccountService.getEmployees().subscribe((userprofile: User) => {
      console.log(userprofile.firstName);
    });

推荐阅读