代码之家  ›  专栏  ›  技术社区  ›  Franki1986

角度6 rxjs 6返回可观察嵌套元素

  •  0
  • Franki1986  · 技术社区  · 7 年前

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

    假设我有一个HttpClient,它可以观察到一些人:

    export class People{
    
        public Name: string;
    }
    

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

    现在我想从我的observable as observable得到这个名字:

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

    这怎么可能呢?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Valeriy Katkov    7 年前

    你可以用 map()

    const nameObservable = this.peopleService.get(id).pipe(
        map(people => people.name)
    );
    
        2
  •  0
  •   Sachila Ranawaka    7 年前

    订阅活动

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