【问题标题】:Angular 6 rxjs 6 return Observable nested elementAngular 6 rxjs 6 返回 Observable 嵌套元素
【发布时间】:2018-08-30 14:37:18
【问题描述】:

我想返回一个可观察对象的子元素。

假设我有一个可以观察到人的 HttpClient:

export class People{

    public Name: string;
}

以及一个将单个服务作为可观察的服务:

public get(id: number): Observable<People> {
    return this.http.get<People>('http://localhost:8801/' + "people/" + id);
  }

现在我想从我的 observable 中获取名称作为 observable:

public get(id: number): Observable<string>{
    this.peopleService.get(id) .... ?
}

这是怎么做到的?

【问题讨论】:

    标签: angular observable rxjs6


    【解决方案1】:

    您可以使用map() 函数来转换可观察的人:

    const nameObservable = this.peopleService.get(id).pipe(
        map(people => people.name)
    );
    

    【讨论】:

      【解决方案2】:

      订阅活动

        this.peopleService.get(id).subscribe((item) => {
         console.log(item.name)
       })
      

      【讨论】:

      • 不,我不想获取名称,我想将名称作为 Observable 返回。具体来说,我想将 async 绑定到 name 属性。
      猜你喜欢
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      相关资源
      最近更新 更多