微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决我正在尝试获取选择器值,将其存储在变量数组中,并使用 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}))
        )
      )
    )
  )
)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。