简体   繁体   English

结合两个依赖的 observables (Rxjs) 的结果

[英]Combine the result of two depending observables (Rxjs)

i have two observables A and B. I first have to get A until i could take B.我有两个可观察的 A 和 B。我首先必须得到 A,直到我可以拿 B。

If they are independently i could do something like forkJoin to get the result.如果他们是独立的,我可以做一些类似 forkJoin 的事情来得到结果。 But due to the fact i could take B only after i got A i am struggeling a little.但由于我只有在得到 A 后才能拿 B,所以我有点挣扎。 I tried switchMap, but seems also not to work.我试过switchMap,但似乎也不起作用。

So what i want to achive is:所以我想要实现的是:

  • get observable A得到可观察的 A
  • get observable B得到可观察的 B
  • combine A and B into a result C将 A 和 B 组合成一个结果 C

     public loadConfiguration(): Observable<ProductConfiguration> { return this.service.getA().pipe( switchMap(response => { return this.service.getB(response.id); } ), map(result => this.getData(result)) // here i want A and B thogether so result should contain A and B ); }

Currently i am a little lost.目前我有点失落。 Regards Oliver问候奥利弗

You can do it like this:你可以这样做:

public loadConfiguration(): Observable<ProductConfiguration> {
  return this.service.getA().pipe(
     switchMap(response => {
      return this.service.getB(response.id).pipe(
        map(responseB => ({responseA: response, responseB})),
      );
     }),
     map(result => this.getData(result)) // result already have responseA & responseB
  );
}

Or this:或这个:

public loadConfiguration(): Observable<ProductConfiguration> {
  return this.service.getA().pipe(
    switchMap(response => {
      return this.service.getB(response.id).pipe(
        map(responseB => this.getData(...)), // you have access to response and responseB here
      );
    }),
  );
}

Try to use zip method to combine both results of both Observable:尝试使用zip方法将两个 Observable 的两个结果结合起来:

public loadConfiguration(): Observable<ProductConfiguration> {
    return this.service.getA().pipe(
        switchMap(response => {
            return Observable.zip(
                this.service.getB(response.id),
                of(response)
            ); 
            return this.service.getB(response.id);
        })),
        .subscribe(([resA, resB]) => {
            console.log('resA', resA);
            console.log('resB', resB);

        }

According to ReactiveX:根据 ReactiveX:

zip combines the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function zip通过指定的 function 将多个 Observable 的发射组合在一起,并根据此 function 的结果为每个组合发射单个项目

Work example can be seen at stackbitz.com. 工作示例可以在 stackbitz.com 中看到。

You can use the second argument of the switchMap (resultSelector) to combine the result:您可以使用switchMap (resultSelector) 的第二个参数来组合结果:

public loadConfiguration(): Observable<ProductConfiguration> {
        return this.service.getA()
                   .pipe(
                       switchMap(response => this.service.getB(response.id), (a, b) => this.getData({a, b})),
                   );
    }

rxjs learn switchMap rxjs学习switchMap

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

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