获取订阅的结果并立即将其应用于另一个订阅

Take a subscribed result and immediate apply it to another subscription

使用 RxJS,我有一种情况需要从 Observable 中检索单个值,然后使用 pluck().

立即将其应用于单独的订阅

我有一个可行的解决方案,但它不够优雅。有没有办法简化或改进流程?

this.fooService.singleValue$.subscribe(value => {
   this.barService.allResults$.pipe(pluck(value)).subscribe(pluckedResult => {
       // do something with the plucked result
   });
});

不推荐使用嵌套订阅(即使用显式订阅)。 在这种情况下最好使用 switchMap/concatMap/mergeMap 运算符,这样您就不需要订阅两次,只需订阅一次即可。

this.fooService.singleValue$.pipe(switchMap((value => {
                    return this.barService.allResults$.pipe(pluck(value));
                }))).subscribe(() => {
                    //do something
                });