angular - NgRx - 在效果中选择 AppState
问题描述
我在我的 NgRx 应用程序中有一个效果,它需要将一个动作作为“副作用”分派,以及该效果应该分派的主要最终动作。但是这个“副作用动作”取决于我需要从 AppState 中选择的状态键。
恐怕我实现的方式不正确,因为总是在 AppState 键更改时调度副作用动作。我知道使用 NgRx 选择器会创建一个 Observables 流,但我不知道对此的正确实现是什么。
这是我的效果:
createNewTask$ = createEffect(() => this.actions$.pipe(
ofType(fromAgendaActions.createNewTask),
concatMap(action => this.agendaService.createNewTask(action.payload)
.pipe(
tap(() => {
this.store.select(selectCurrentDate).subscribe(currentDate => {
if(moment(action.payload.followUpDate).isBefore(moment().startOf('date'))) {
this.store.dispatch(fromAgendaActions.getOverdueTasks());
}
if(moment(currentDate).startOf('date').isSame(moment(action.payload.followUpDate))) {
this.store.dispatch(fromAgendaActions.getTasks({ payload: { date: moment(currentDate).format('DD-MM-YYYY') }}));
}
});
}),
map(() => fromAgendaActions.createNewTaskSuccess()),
catchError(() => of({ type: fromAgendaActions.AgendaActionsTypes.CreateNewTaskFail, error: 'error' }))
)
)));
解决方案
没试过,但以下应该没问题:
createNewTask$ = createEffect(() =>
this.actions$.pipe(
ofType(fromAgendaActions.createNewTask),
withLatestFrom(this.store.select(selectCurrentDate)),
concatMap(([action, currentDate]) =>
this.agendaService.createNewTask(action.payload).pipe(
concatMap(() => {
const actions = [];
if (
moment(action.payload.followUpDate).isBefore(
moment().startOf('date')
)
) {
actions.push(fromAgendaActions.getOverdueTasks());
}
if (
moment(currentDate)
.startOf('date')
.isSame(moment(action.payload.followUpDate))
) {
actions.push(
fromAgendaActions.getTasks({
payload: { date: moment(currentDate).format('DD-MM-YYYY') },
})
);
}
actions.push(fromAgendaActions.createNewTaskSuccess());
return from(actions).map(a => of(a));
}),
catchError(() =>
of({
type: fromAgendaActions.AgendaActionsTypes.CreateNewTaskFail,
error: 'error',
})
)
)
)
)
);
推荐阅读
- arrays - For循环正在接受数组中的第一个对象并忽略其他对象
- jquery - slideDown 有效,但是当我更改为 slideUp 时,它停止工作
- apache-spark - Avro -> Parquet -> Spark SQL
- laravel-5 - Laravel 文件上传失败,出现 419 错误(会话已过期)
- asp.net-mvc - 在一个操作上覆盖全局 AutoValidateAntiforgeryTokenAttribute
- c - Passing punctuaton marks through command line in C
- c# - toSet.SetValue 仅采用数组中的第一个选项
- python - 在 Python 中计算自相关作为滞后的函数
- react-native - 升级 React Native 后捆绑失败错误
- tensorflow - 微调BERT的最后x层