如何在 rxjs 中将动作数据传递到可管道运算符流的下游?

How to pass action data downstream a pipeable operator stream in rxjs?

我有一种情况想在第三级操作中访问动作负载。 我可以在 lettable operators 中做这样的事情,但是我怎么能用 pipeable operator 做同样的事情呢?

这是我的代码,

 @Effect()
  onTrySignin = this.actions$.pipe(
    ofType(AuthActions.TRY_SIGNIN),
    map((action: AuthActions.TrySignin) => {
      return action.payload;
    }),
    switchMap(action => {
      return this.httpService
        .postRequest('UserAccounts/Login', action.credentials);
    }), catchError((error: HttpErrorResponse) => {
      return Observable.of(new AuthActions.FailedAuth(error));
    }),
    mergeMap((response: any) => {
      // how to access action payload here?
    })
  );

您可以使用 map() 沿着可观察链传递数据,如下所示:

// both foo and bar will be available on next()
from(AsyncFooData()).pipe(
  concatMap(foo => AsyncBarData().pipe(
    map(bar => ({foo, bar})
  )),
  tap(val => console.log(val), // chain more operators here...
).subscribe(({foo, bar}) => {
  // do stuff with foo and bar
})

FWIW,我从 那里得到了这个答案,我在那里发布了一个有点相似的答案。

好的,它是 pipe 里面的 pipe

 @Effect()
  onTrySignin = this.actions$.pipe(
    ofType(AuthActions.TRY_SIGNIN),
    map((action: AuthActions.TrySignin) => {
      return action.payload;
    }),
    switchMap(actionPayload => {
      return this.httpService.postRequest('UserAccounts/Login', actionPayload.credentials).pipe(
        mergeMap((response: HttpResponse<IApiResponder<string>>) => {
          switch (response.status) {
            case 200:
              if (actionPayload.returnUrl) {
                this.router.navigate([actionPayload.returnUrl]);
              } else {
                this.router.navigate(['/dbapp']);
              }
              return Observable.concat(
                Observable.of(new AuthActions.GenerateAntiforgeryToken()),
                Observable.of(new AuthActions.Signin(this.authService.getUserData())),
              );
          }
        }),
        catchError(e => {
          return Observable.of(new AuthActions.FailedAuth(e));
        }),
      );
    }),
  );