ngrx angular9 从商店获取数组
ngrx angular9 getting array from store
我正在努力从我的组件内的 NGRX 存储中获取数据。
我的 redux 有以下配置:
calendar.reducer.ts:
import {MonthModel} from "./month.model";
import * as MonthActions from "./calendar.actions"
export const initialState: MonthModel = {
shifts: [],
numberOfYear: 2020,
numberOfMonth: 6,
loading: false
};
export type Action = MonthActions.All;
export function calendarReducer(state: MonthModel = initialState, action: Action) {
switch (action.type) {
case MonthActions.GET_SHIFTS:
return {...state, loading: true};
case MonthActions.GET_SHIFTS_SUCCESS:
return {...state, ...action.payload ,loading: false};
default: state;
}
return state;
}
calendar.effects.ts
import { Injectable } from "@angular/core";
import {act, Actions, Effect, ofType} from "@ngrx/effects";
import {Observable, of} from "rxjs";
import {catchError, delay, map, mergeMap, switchMap} from 'rxjs/operators';
import * as MonthActions from "./calendar.actions"
import {ShiftsService} from "../service/shifts.service";
export type Action = MonthActions.All;
@Injectable()
export class CalendarEffects {
constructor( private actions: Actions,
private shiftService: ShiftsService) {}
@Effect()
getShifts = this.actions.pipe(ofType(MonthActions.GET_SHIFTS),
map((action: MonthActions.getShiftsAction) => action.payload),
delay(2000),
mergeMap(payload => this.shiftService.getShifts(2020, 6).pipe (
map(getShiftsState => ({type: MonthActions.GET_SHIFTS_SUCCESS, payload:
getShiftsState})),
catchError(() => of({type: MonthActions.GET_SHIFTS_ERROR})))))
}
import {MonthModel} from "./month.model";
import {createFeatureSelector, createSelector} from "@ngrx/store";
export interface MonthState {
monthState: MonthModel;
}
export const getMonthShifts = (state: MonthState) => state.monthState.shifts ;
export const getMonthLoading = (state: MonthState) => state.monthState;
export const calendarStateFeatureKey = 'MonthModel';
export const getMonthState = createFeatureSelector(calendarStateFeatureKey);
export const getCalendarLoading = createSelector(getMonthState, (state:MonthModel)=> state.loading);
export const getCalendarShifts = createSelector(getMonthState, (state:MonthModel)=> state.shifts );
calendar.selectors.ts
import {MonthModel} from "./month.model";
import {createFeatureSelector, createSelector} from "@ngrx/store";
export interface MonthState {
monthState: MonthModel;
}
export const getMonthShifts = (state: MonthState) => state.monthState.shifts ;
export const getMonthLoading = (state: MonthState) => state.monthState;
export const calendarStateFeatureKey = 'MonthModel';
export const getMonthState = createFeatureSelector(calendarStateFeatureKey);
export const getCalendarLoading = createSelector(getMonthState, (state:MonthModel)=> state.loading);
export const getCalendarShifts = createSelector(getMonthState, (state:MonthModel)=> state.shifts );
calendar.actions.ts
import {Action} from "@ngrx/store";
import {MonthModel} from "./month.model";
export const GET_SHIFTS = "GET_SHIFTS";
export const GET_SHIFTS_SUCCESS = "GET_SHIFTS_SUCCESS";
export const GET_SHIFTS_ERROR = "GET_SHIFTS_ERROR";
export class getShiftsAction implements Action {
readonly type = GET_SHIFTS;
constructor(public payload: string) { }
}
export class getShiftsSuccessAction implements Action {
readonly type = GET_SHIFTS_SUCCESS;
constructor(public payload: MonthModel[]) { }
}
export type All
= getShiftsAction
| getShiftsSuccessAction;
month.model.ts
import {Shift} from "../shift/shift";
export interface MonthModel {
loading: boolean;
numberOfMonth: number;
numberOfYear: number;
shifts : Shift[];
error?: string;
}
我正在尝试通过选择器从 MonthModel 获取数据,这个工作正常,如下所示:
this.monthState$ = this.store.select(getCalendarLoading);
但我无法从商店获取 shifts 数组!我只得到空数组!
this.monthShifts$ = this.store.select(getCalendarShifts);
this.monthShifts$.subscribe(
shifts=>{
console.log('data',shifts );
}
)
请帮助我理解并修复:如何从商店获取班次数组?
p.s。在 ReduxDevTools 中,我可以看到我存储的所有数据,所有班次都在那里。谢谢!
很简单,你忘了把action
的payload
处理给reducer
case MonthActions.GET_SHIFTS:
return {...state, loading: true}; // <-- here we need action.payload
// return {...state, shifts: aciton.payload, loading: true}, // I guess
我正在努力从我的组件内的 NGRX 存储中获取数据。 我的 redux 有以下配置: calendar.reducer.ts:
import {MonthModel} from "./month.model";
import * as MonthActions from "./calendar.actions"
export const initialState: MonthModel = {
shifts: [],
numberOfYear: 2020,
numberOfMonth: 6,
loading: false
};
export type Action = MonthActions.All;
export function calendarReducer(state: MonthModel = initialState, action: Action) {
switch (action.type) {
case MonthActions.GET_SHIFTS:
return {...state, loading: true};
case MonthActions.GET_SHIFTS_SUCCESS:
return {...state, ...action.payload ,loading: false};
default: state;
}
return state;
}
calendar.effects.ts
import { Injectable } from "@angular/core";
import {act, Actions, Effect, ofType} from "@ngrx/effects";
import {Observable, of} from "rxjs";
import {catchError, delay, map, mergeMap, switchMap} from 'rxjs/operators';
import * as MonthActions from "./calendar.actions"
import {ShiftsService} from "../service/shifts.service";
export type Action = MonthActions.All;
@Injectable()
export class CalendarEffects {
constructor( private actions: Actions,
private shiftService: ShiftsService) {}
@Effect()
getShifts = this.actions.pipe(ofType(MonthActions.GET_SHIFTS),
map((action: MonthActions.getShiftsAction) => action.payload),
delay(2000),
mergeMap(payload => this.shiftService.getShifts(2020, 6).pipe (
map(getShiftsState => ({type: MonthActions.GET_SHIFTS_SUCCESS, payload:
getShiftsState})),
catchError(() => of({type: MonthActions.GET_SHIFTS_ERROR})))))
}
import {MonthModel} from "./month.model";
import {createFeatureSelector, createSelector} from "@ngrx/store";
export interface MonthState {
monthState: MonthModel;
}
export const getMonthShifts = (state: MonthState) => state.monthState.shifts ;
export const getMonthLoading = (state: MonthState) => state.monthState;
export const calendarStateFeatureKey = 'MonthModel';
export const getMonthState = createFeatureSelector(calendarStateFeatureKey);
export const getCalendarLoading = createSelector(getMonthState, (state:MonthModel)=> state.loading);
export const getCalendarShifts = createSelector(getMonthState, (state:MonthModel)=> state.shifts );
calendar.selectors.ts
import {MonthModel} from "./month.model";
import {createFeatureSelector, createSelector} from "@ngrx/store";
export interface MonthState {
monthState: MonthModel;
}
export const getMonthShifts = (state: MonthState) => state.monthState.shifts ;
export const getMonthLoading = (state: MonthState) => state.monthState;
export const calendarStateFeatureKey = 'MonthModel';
export const getMonthState = createFeatureSelector(calendarStateFeatureKey);
export const getCalendarLoading = createSelector(getMonthState, (state:MonthModel)=> state.loading);
export const getCalendarShifts = createSelector(getMonthState, (state:MonthModel)=> state.shifts );
calendar.actions.ts
import {Action} from "@ngrx/store";
import {MonthModel} from "./month.model";
export const GET_SHIFTS = "GET_SHIFTS";
export const GET_SHIFTS_SUCCESS = "GET_SHIFTS_SUCCESS";
export const GET_SHIFTS_ERROR = "GET_SHIFTS_ERROR";
export class getShiftsAction implements Action {
readonly type = GET_SHIFTS;
constructor(public payload: string) { }
}
export class getShiftsSuccessAction implements Action {
readonly type = GET_SHIFTS_SUCCESS;
constructor(public payload: MonthModel[]) { }
}
export type All
= getShiftsAction
| getShiftsSuccessAction;
month.model.ts
import {Shift} from "../shift/shift";
export interface MonthModel {
loading: boolean;
numberOfMonth: number;
numberOfYear: number;
shifts : Shift[];
error?: string;
}
我正在尝试通过选择器从 MonthModel 获取数据,这个工作正常,如下所示:
this.monthState$ = this.store.select(getCalendarLoading);
但我无法从商店获取 shifts 数组!我只得到空数组!
this.monthShifts$ = this.store.select(getCalendarShifts);
this.monthShifts$.subscribe(
shifts=>{
console.log('data',shifts );
}
)
请帮助我理解并修复:如何从商店获取班次数组? p.s。在 ReduxDevTools 中,我可以看到我存储的所有数据,所有班次都在那里。谢谢!
很简单,你忘了把action
的payload
处理给reducer
case MonthActions.GET_SHIFTS:
return {...state, loading: true}; // <-- here we need action.payload
// return {...state, shifts: aciton.payload, loading: true}, // I guess