简体   繁体   English

Angular RXJS,如何在使用多个mergeMap调用(顺序)后获取订阅中的所有数据?

[英]Angular RXJS, how do I get all data in subscribe after using multiple mergeMap calls (sequentially)?

I'd like to get all data similar to how 'forkJoin' does it after making multiple HTTP calls sequentially using MergeMap.在使用 MergeMap 按顺序进行多个 HTTP 调用后,我想获取类似于“forkJoin”的所有数据。

this.dataService.getData1().pipe(
      mergeMap(response1 => this.dataService.getData2()),
      mergeMap(response2 => this.dataService.getData3()),
      mergeMap(response3 => this.dataService.getData4()),
    ).subscribe(

    //How do I get response1, response2, response3 and response4 here?

 )

If no observable's parameter depend on the previous call如果没有 observable 的参数依赖于之前的调用

you can use concat , which will play the observables is a sequence, each time waiting on the previous to finish (unlike merge )您可以使用concat ,它将播放可观察对象是一个序列,每次都等待前一个完成(不像merge

var arr = [
  this.dataService.getData1(),
  this.dataPublicationsService.getData2(),
  this.dataPublicationsService.getData3(),
  this.dataPublicationsService.getData4(),
];
concat(...arr).pipe(toArray()).subscribe((a) => console.log(a));

If each call depends on the previous one there is no "clean" way to do it, you have to pass the data youself.如果每个调用都依赖于前一个调用,则没有“干净”的方法,您必须自己传递数据。

One way to achieve this is use forkJoin and the spread operator实现此目的的一种方法是使用forkJoin和传播运算符

class Foo {
  dataService: { getData1: () => Observable<number> };
  dataPublicationsService: {
    getData2: () => Observable<string>;
    getData3: () => Observable<number>;
    getData4: () => Observable<object>;
  };

  bar() {
    this.dataService
      .getData1()
      .pipe(
        mergeMap((response1) =>
          forkJoin([of(response1), this.dataPublicationsService.getData2()])
        ),
        mergeMap((rest) => // rest is [number, string]
          forkJoin([of([...rest]), this.dataPublicationsService.getData3()])
        ),
        mergeMap((rest) => rest is [number, string, number]
          forkJoin([of(...rest), this.dataPublicationsService.getData4()])
        )
      )
      .subscribe((rest) => {
         // [number, string, number, object]
      });
   }
}

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

相关问题 Angular &amp; RxJS - 使用 .mergeMap 组合多个 XHR 服务调用 - Angular & RxJS - Combining Multiple XHR Service calls using .mergeMap angular rxjs mergeMap/flatMap 而不是多个管道/订阅 - angular rxjs mergeMap/flatMap instead of multiple pipe/subscribe Angular 8 RXJS - 按顺序进行多个 HTTP 调用 - Angular 8 RXJS - Make multiple HTTP calls sequentially RxJS observable 在 mergeMap 后不会触发订阅 - RxJS observable does not trigger subscribe after mergeMap 在RxJS mergeMap之间导航时如何避免多次HttpInterceptor调用? - How to avoid multiple HttpInterceptor calls while navigating between RxJS mergeMap? angular rxjs 在没有订阅的情况下发送所有 observable 之后做一些事情 - angular rxjs do something after all observable sended without subscribe 将 MergeMap 与从另一个 observable 接收的数据数组一起使用 - RxJs Angular - Using MergeMap with the array of data received from another observable - RxJs Angular 如何使用 RxJS 并行化多个 HTTP 调用 - How do I parallelise multiple HTTP calls using RxJS 我怎么知道谁叫 RXJS MergeMap 响应 - How do i know who called RXJS MergeMap response 为什么在我的angular 2项目中收到太多rxjs文件调用? (似乎都是他们) - Why do i get too many rxjs file calls in my angular 2 project? (seems to be all of them)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM