Angular NGRX : multiple Entities in one EntityAdapter possible?

标签 angular ngrx

最近引入了 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/

相关文章:

angular - NGRX 流程和路由 - 组件被访问次数过多

typescript - 当应用程序在 Angular 2 中启动时如何运行服务

Angular ngrx@effect 基本问题 : Type 'Observable<void>' is not assignable to type

angularjs - ngrx 参数选择函数

angular - 如何将 Angular 2 数据网格绑定(bind)到 Angular 4 FormArray

angular - 多个连续效果未完成?

angular - 如何在 nrgx 8 单元测试中模拟选择器?

html - 更改 mat-paginator 的 [pageSizeOption] 字体大小和字体系列

unit-testing - Angular2 RC5 如何正确配置测试模块

angular - Observable.next() 不是函数