如何用管道方法从一个observable中提取一个observable?

时间:2018-02-06 13:16:57

标签: angular typescript rxjs

如何在管道方法中提取可观察的结果?我有以下内容:

// type of "observables" is Observable<Observable<MyType>[]>
const observables = this.http.get<MyTypeList>(rootUrl).pipe(
  map(list => list[locale]), 
  map(urlList => urlList.map(url => this.http.get<MyType>(url))),
  // I need some method here to make Observable<MyType>[] from Observable<Observable<MyType>[]>
);

forkJoin(observables).subscribe(/* I have all my items here*/)

在第二次map通话后,我需要一些方法从Observable<MyType>[]制作Observable<Observable<MyType>[]>这是否有一种优雅的方法?

1 个答案:

答案 0 :(得分:0)

问题是我曾尝试在外面使用forkJoin,我想我可以在建议的mergeMap里面使用它。

const obs = this.http
  .get<MyTypeList>(rootUrl)
  .pipe(
    map(list => list[locale]), 
    mergeMap(urlList =>
      forkJoin(
        urlList.packages.map(url => this.http.get<MyType>(url))
      )
    )
  );

obs.subscribe(/* I have all my items here*/);