简体   繁体   English

如何为 ngrx 商店应用程序设置初始 state

[英]How to set initial state for ngrx store application

I am new to ngrx and am trying to manage my state with it.In my application, every agent(staff) have a group of customers that are tied to him/her.我是 ngrx 的新手,正在尝试用它管理我的 state。在我的应用程序中,每个代理(员工)都有一组与他/她相关的客户。 I am trying to set the initial state of each agent objects and i don't know how.我正在尝试设置每个代理对象的初始 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;
    };
  };
}

Each agent object should have some initial state.每个代理 object 应该有一些初始值 state。

something like this像这样的

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: "",
    },
  },
};

Edit: This is an example of what should be in the store if all goes well.编辑:这是一个示例,说明如果一切顺利,商店中应该有什么。

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: "",
    },
  },

What i want is to be able to set the initial state of each subsequent object i add to the store.我想要的是能够设置我添加到商店的每个后续 object 的初始 state。

You should use the Store Module for this-您应该为此使用商店模块-

import {createAction, props} from '@ngrx/store'

Refer to the official guide- https://ngrx.io/guide/store https://ngrx.io/guide/store/reducers参考官方指南- https://ngrx.io/guide/store https://ngrx.io/guide/store/reducers

Please check out this gist - https://github.com/ngrx/platform/issues/662请查看此要点 - https://github.com/ngrx/platform/issues/662

Try to pass the type, then set the initialState of your reducer尝试传递类型,然后设置 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM