繁体   English   中英

Angular 6 rxjs 6返回Observable嵌套元素

[英]Angular 6 rxjs 6 return Observable nested element

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

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

export class People{

    public Name: string;
}

以及一项可获得的服务:

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

现在,我想从我的可观察名称中获取可观察名称:

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

怎么办呢?

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

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

订阅活动

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM