首页 > 解决方案 > 在商店中拉取 Ngrx 实体的问题

问题描述

我一直在开发一个应用程序来学习 Angular Ngrx 商店和实体。该应用程序使用来自 ngrx/platform 的示例应用程序作为起点。代码和应用程​​序在 StackBlitz 上

我正在尝试使用 createSelector 过滤掉具有与所选任务的 id 匹配的 task_id 的 pomos。甚至无法做到这一点,我尝试使用基于 EntityAdapter 的 pomo reducer 将所有 pomos 加载到 state 中。那也行不通。我的所有操作都成功了,包括向商店添加了一个 pomo,但我无法加载它们。在这一点上,我只是想让它们全部加载,但最终目标是一个选择器,它根据它的 task_id 加载 pomos。我怀疑这个问题很可能是国家实际上是如何组成的,但我不知道该怎么做。就像我说的,将 Pomo 添加到商店的操作可以正常工作,但是在加载 pomo 时。它返回空。

这是我的减速器/index.ts

export interface State {
  search: fromSearch.State;
  tasks: fromTasks.State;
  pomos: fromPomos.State;
  collection: fromCollection.State;
}

export const reducers: ActionReducerMap<any> = {
  search: fromSearch.reducer,
  tasks: fromTasks.reducer,
  pomos: fromPomos.reducer,
  collection: fromCollection.reducer,
};

export const getTasksState = createFeatureSelector<TasksState>('tasks');
export const getPomosState = createSelector(getTasksState, state => state.pomos);


export const getTaskEntitiesState = createSelector(
  getTasksState,
  state => state.tasks
);


export const getPomosEntitiesState = createSelector(
  getTasksState,
  state => state.pomos
);

export const {
  selectIds: getTaskIds,
  selectEntities: getTaskEntities,
  selectAll: getAllTasks,
  selectTotal: getTotalTasks,
} = fromTasks.adapter.getSelectors(getTaskEntitiesState);

export const {
  selectIds: getPomoIds,
  selectEntities: getPomoEntities,
  selectAll: getAllPomos,
  selectTotal: getTotalPomos,
} = fromPomos.adapter.getSelectors(getPomosEntitiesState);

标签: angularngrxngrx-storengrx-entity

解决方案


推荐阅读