我正在尝试获取选择器值,将其存储在变量数组中并使用 ngrx 将此数组传递给 account.effect.ts 文件中的成功操作
i am trying to get selector value, store it in a variable array and pass this array to a success action in account.effect.ts file using ngrx
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}))
)
)
)
)
)
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}))
)
)
)
)
)