我正在尝试获取选择器值,将其存储在变量数组中,并使用 ngrx 将此数组传递给 account.effect.ts 文件中的成功操作

问题描述

account.effect.ts 文件

fetchUserInfo$ = createEffecct(() =>
  this.actions$.pipe(
    ofType(AccountActions._fetchAccountInfo),switchMap(({accountNumber}) =>
      this.accServvice.getAccInfo(accountNum).pipe(
        map((accountInfo: AccType) =>
          AccountActions.loadUserInfoSuccess({data: accountInfo}),),catchError((error) =>
          of(AccountActions.userDataFails({error}))
        )
      )
    )
  )
)

在上面的文件中,我导入了选择器,您可以通过以下方式访问它 this.store.select(FromAcc.getSelectedAccInfo)。我想访问选择器值 FromAcc.getSelectedAccInfo 并添加一个我将从服务接收的值,然后将其发送到操作 AccountActions.loadUserInfoSuccess。 我对 ngrx 很陌生。请告诉我该怎么做。

解决方法

您可以尝试以下解决方案

fetchUserInfo$ = createEffecct(() =>
  this.actions$.pipe(
    ofType(AccountActions._fetchAccountInfo),// below 2 lines updated
    withLatestFrom(this.store.select(FromAcc.getSelectedAccInfo)),switchMap(([accountNumber,selectedAccInfo]) =>
      this.accServvice.getAccInfo(accountNum).pipe(
        map(([accountInfo: AccType,) =>
          AccountActions.loadUserInfoSuccess({data: accountInfo}),),catchError((error) =>
          of(AccountActions.userDataFails({error}))
        )
      )
    )
  )
)