我如何从订户对象获取信息

时间:2019-02-06 16:31:29

标签: angular typescript

我正在使用Subscriber方法,正在获取一个Subscriber对象。 但是我需要的是其中的一个对象,这是一张图片-https://imgur.com/a/4DfsYZT 那我该怎么办呢?

我的服务内部方法:

getUserInfo(name: any): Observable <User> {
const url = `https://api.github.com/users/${name}`;
return this.http.get <User> (url);
}

我的组件内部有什么

public userInfo;

this.userInfo = this.githubService.getUserInfo(this.userLogin)
  .subscribe(info => this.userInfo = info);

3 个答案:

答案 0 :(得分:1)

您必须在订阅方法之外删除对userInfo变量所做的赋值。

this.githubService.getUserInfo(this.userLogin)
  .subscribe(info => this.userInfo = info);

答案 1 :(得分:0)

您必须使用映射功能映射您的响应。 public userInfo;

this.userInfo = this.githubService.getUserInfo(this.userLogin)
  .map(response => response.json())
  .subscribe(info => this.userInfo = info);

this.userInfo = this.githubService.getUserInfo(this.userLogin)
  .map(response => response = response.userInfo)
  .subscribe(info => this.userInfo = info);

答案 2 :(得分:0)

请勿将您的订阅分配给您的userInfo,然后在您订阅时再次重新分配。

尝试:

public subscription;
public userInfo;

this.subscription = this.githubService.getUserInfo(this.userLogin)
                        .subscribe(info => this.userInfo = info);
相关问题