gpt4 book ai didi

Angular NGRX : multiple Entities in one EntityAdapter possible?

转载 作者:太空狗 更新时间:2023-10-29 17:06:08 25 4
gpt4 key购买 nike

最近引入了 NGRX/Entities:

https://medium.com/ngrx/introducing-ngrx-entity-598176456e15 https://github.com/ngrx/platform/tree/master/example-app

并且由于它们是以适配器处理(读取:一个,单个)映射数据结构并在初始化时获取其余的 reducer 状态的方式制作的,我想知道...

是否可以在一个 reducer /适配器中容纳多个实体?界面说没有,但也许有黑客攻击或计划在未来使用?如果我在一个 reducer 中已经有多个映射怎么办?我是被迫将其拆分还是避免使用实体功能?

以下答案有效。模块的另一种方法(在此示例中为延迟加​​载,但不一定)是将 reducer 与 ActionReducerMap 结合使用:

在你的 lazy.module.ts 中:

export interface LazyState {
lazyAState: LazyAState;
lazyBState: LazyBState;
}

export const lazyReducers: ActionReducerMap<LazyState> = {
lazyA: lazyAReducer,
lazyB: lazyBReducer
};

export interface AppState extends forRoot.AppState {
lazy: LazyState;
}

@NgModule({
imports: [
LazyRoutingModule,
StoreModule.forFeature('lazy', lazyReducers),
EffectsModule.forFeature([LazyEffects])
]
})
export class LazyModule {
static forRoot() {
return {
ngModule: LazyModule,
providers: [],
};
}
}

在 lazy.selectors.ts 中(从 reducer 文件中导入适配器):

export const getLazyState = createFeatureSelector<LazyState>('lazy');

const getLazyAState = createSelector(getLazyState, (state: LazyState) => state.lazyAState);
const getLazyBState = createSelector(getLazyState, (state: LazyState) => state.lazyBState);

const {selectEntities: lazyASelectEntities, selectAll: lazyASelectAll} = LAZY_A_ADAPTER.getSelectors();

export const lazyADictionary = createSelector(getLazyAState, lazyASelectEntities);
export const lazyAArray = createSelector(getLazyAState, lazyASelectAll);
export const lazyASomeOtherAttributeNotFromAdapter = createSelector(getLazyAState, (state: LazyAState) => state.ids as string[]);

const {selectEntities: lazyBSelectEntities, selectAll: lazyBSelectAll} = LAZY_B_ADAPTER.getSelectors();
// same as for lazy-A, you can also combine selectors if you want

最佳答案

NgRx 实体是一个简单的小型库,可以处理大型数组,即使文档没有解释如何在一个状态下使用多个实体,这应该很容易,因为库在幕后所做的只是规范化数组并用数据创建一个字典。

为了与一个或多个实体一起工作,请遵循以下步骤:

首先定义每个实体的状态。

interface CarState extends EntityState<Car> {
total: number;
}

interface PlaceState extends EntityState<Place> {
total: number;
}

然后创建一个保存实体的状态

export interface State {
msg: string;
cars: CarState;
places: PlaceState;
}

为每个实体状态创建适配器以操作数据并创建初始状态。

const adapterCar = createEntityAdapter<Car>();
const adapterPlace = createEntityAdapter<Place>();

const carInitialState: CarState = adapterCar.getInitialState({ total: 0 });
const placeInitialState: PlaceState = adapterPlace.getInitialState({ total: 0 });

定义初始全局状态

const initialState = {
msg: 'Multiple entities in the same state',
cars: carInitialState,
places: placeInitialState
}

创建 reducer :

export function reducer(state: State = initialState, action: ExampleActions): State {

switch (action.type) {

case ExampleActionTypes.GetCarList:
return { ...state, cars: adapterCar.addMany(action.payload, state.cars) };

case ExampleActionTypes.GetPlaceList:
return { ...state, places: adapterPlace.addMany(action.payload, state.places) };

default:
return state;
}

}

公开选择器

export const selectCarState = (state: State) => state.cars;
export const selectPlaceState = (state: State) => state.places;

export const { selectAll: selectAllCars } = adapterCar.getSelectors();
export const { selectAll: selectAllPlaces } = adapterPlace.getSelectors();

就是这样:)

实例: https://stackblitz.com/edit/angular-multiple-entities-in-same-state

关于 Angular NGRX : multiple Entities in one EntityAdapter possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com