Angular 中的链式 Observable RxJs

Arn*_*rno 1 rxjs angular rxjs-observables

我使用 RxJs 6.6.0 和 Angular 9。

我有两个函数返回 Observable<'Class1'> 和 Observable<'number'>。

所以我习惯于:

funcA().subscribe(x=>{ // do something...});

funcB().subscribe(x=>{// do somethings..});
Run Code Online (Sandbox Code Playgroud)

但是我只需要在 funcA() 完成时调用 funcB 。我看到 concat 可能会有所帮助,但我无法得到这样的两个答案

concat(funcA,funcB).subscribe( (x,y)=>{// do with x
//do with y});
Run Code Online (Sandbox Code Playgroud)

我想这样做是因为 funcB 的参数取决于 funcA 的返回。

Var*_*tel 5

使用switchMapfrom rxjs/operators 来实现。

this.sub = funcA()
  .pipe(
    switchMap(valueFromA => {
      // use valueFromA in here.
      return funcB()
    })
  )
  .subscribe();
Run Code Online (Sandbox Code Playgroud)