Angular NgRx-选择有效的AppState
我在我的NgRx应用程序中有一个效果,它需要将一个动作作为“副作用”与该效果应该发送的主要最终动作一起发送。但是这个“副作用操作”取决于我需要从AppState中选择的状态键 我担心我的实现方式是不正确的,因为当AppState键更改时,总是调度副作用操作。我知道使用NgRx选择器会创建一个可观察的流,但我不知道正确的实现方法是什么 以下是我的效果:Angular NgRx-选择有效的AppState,angular,rxjs,ngrx,Angular,Rxjs,Ngrx,我在我的NgRx应用程序中有一个效果,它需要将一个动作作为“副作用”与该效果应该发送的主要最终动作一起发送。但是这个“副作用操作”取决于我需要从AppState中选择的状态键 我担心我的实现方式是不正确的,因为当AppState键更改时,总是调度副作用操作。我知道使用NgRx选择器会创建一个可观察的流,但我不知道正确的实现方法是什么 以下是我的效果: createNewTask$ = createEffect(() => this.actions$.pipe( ofType(fromA
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',
})
)
)
)
)
);
很高兴听到这个消息:)