[英]How to set initial state for ngrx store application
我是 ngrx 的新手,正在尝试用它管理我的 state。在我的应用程序中,每个代理(员工)都有一组与他/她相关的客户。 我正在尝试设置每个代理对象的初始 state,但我不知道如何设置。
import { createReducer } from "@ngrx/store";
import { Cursor } from "../../../models/cursor";
import { Customer } from "../../../models/customer";
export interface State {
agent_customer: {
[agentId: number]: {
customer: Customer[];
cursors: Cursor;
total: number;
loading: boolean;
errorMessage: string;
items_per_page: number;
pageSizeOptions: number[];
pageIndex: number;
searchKey: string;
};
};
}
每个代理 object 应该有一些初始值 state。
像这样的
export const initialState: State = {
agent_customer: {
1: {
customer: [],
cursors: {
after: "",
before: "",
hasNext: false,
hasPrevious: false,
},
total: 0,
loading: false,
errorMessage: null,
items_per_page: 2,
pageSizeOptions: [2, 3, 5, 10, 15],
pageIndex: 0,
searchKey: "",
},
},
};
编辑:这是一个示例,说明如果一切顺利,商店中应该有什么。
agent_customer: {
198282: {
customer: [],
cursors: {
after: "",
before: "",
hasNext: false,
hasPrevious: false,
},
total: 0,
loading: false,
errorMessage: null,
items_per_page: 2,
pageSizeOptions: [2, 3, 5, 10, 15],
pageIndex: 0,
searchKey: "",
},
165436: {
customer: [],
cursors: {
after: "",
before: "",
hasNext: false,
hasPrevious: false,
},
total: 0,
loading: false,
errorMessage: null,
items_per_page: 2,
pageSizeOptions: [2, 3, 5, 10, 15],
pageIndex: 0,
searchKey: "",
},
981342: {
customer: [],
cursors: {
after: "",
before: "",
hasNext: false,
hasPrevious: false,
},
total: 0,
loading: false,
errorMessage: null,
items_per_page: 2,
pageSizeOptions: [2, 3, 5, 10, 15],
pageIndex: 0,
searchKey: "",
},
},
我想要的是能够设置我添加到商店的每个后续 object 的初始 state。
您应该为此使用商店模块-
import {createAction, props} from '@ngrx/store'
参考官方指南- https://ngrx.io/guide/store https://ngrx.io/guide/store/reducers
尝试传递类型,然后设置 reducer 的 initialState
import { Action, createFeatureSelector, createReducer, on } from '@ngrx/store';
import { ACTION } from './actions';
import { Entity} from './entity';
export interface CustomState {
agent: Entity[] // Pass the entity type, on this case Entity[]
}
// type the initial state with your interface(state)
const initialState: CustomState = {
agent: [
// Pass here each object of type (Entity)
{
total: 0,
loading: false,
errorMessage: null,
items_per_page: 2,
...
}
]
}
const reducer = createReducer(
initialState,
on(ACTION, (state, { payload }) => ({ ...state, agent: payload }))
);
export function customReducer(state: CustomState, action: Action) {
return reducer(state, action)
};
export const getCustomState = createFeatureSelector<CustomState>('customName');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.