如何将forkJoin中一个API调用的响应用作另一个API调用的参数?

时间:2019-02-27 10:30:30

标签: angular rxjs

我想用b的响应作为参数进行c调用。如何实现呢?

resolve(
            route: ActivatedRouteSnapshot,
            state: RouterStateSnapshot
        ): Observable<any> {

            let a = this.loginService.getApplicationCentre();
            let b = this.loginService.getLoginId();
            let c = this.loginService.getUserDetails(response of (b))

            let join = forkJoin(a, b, c).pipe(map((allResponses) => {
                return {
                    A: allResponses[0],
                    B: allResponses[1],
                    C: allResponses[2]
                };
            }));

            return join;

        }

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

这可以如下进行:

let a$ = this.loginService.getApplicationCentre();
let bc$ = this.loginService.getLoginId()
  .pipe(
    mergeMap(b => this.loginService.getUserDetails(b)
      .pipe(map(c => [b, c])))
  );
return forkJoin(a$, bc$).pipe(map(([A, [B, C]]) => ({ A, B, C })));