0

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

这是我的代码,

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

2 回答 2

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,我从这个问题中得到了这个答案,在那里我发布了一个类似的答案。

于 2018-06-27T22:56:10.890 回答
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));
        }),
      );
    }),
  );
于 2018-06-28T00:23:11.553 回答