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

时间:2018-06-27 21:58:10

标签: rxjs rxjs-pipeable-operators

我遇到一种情况,我想在第三级操作中访问动作有效负载。 我可以在可操作的运算符中进行这种操作,但是如何使用可管道运算符进行相同操作?

这是我的代码,

 @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?
    })
  );

2 个答案:

答案 0 :(得分:1)

您可以使用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,我从this question那里得到了这个答案,我在其中发布了一个类似的答案。

答案 1 :(得分:0)

好吧,它是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));
        }),
      );
    }),
  );