angular - 更新 EntityAdapter 内的数组

标签 angular ngrx

我有以下EntityAdapter

export interface IDroneRoute {
  assignedDroneId?: number;
  routeId: string;
  rerouting?: boolean;
  waypoints: Waypoint[];
}

export const featureAdapter: EntityAdapter<IDroneRoute> = createEntityAdapter<IDroneRoute>({
  selectId: model => model.routeId,
});

我想要一个在数组中添加、删除和删除航路点的操作

const ADD_ROUTE_POINT = (state: State, action: any) => {
  return featureAdapter.updateOne({
    id: action.payload.routeId,
    changes: {// add waypoint},
  }, state);
};

如何访问更改中项目的当前数组以便更新它?

最佳答案

changes 对应于您要更新的实体 (IDroneRoute),因此在返回 featureAdapter.updateOne({) 之前,您可以构建您的更新的对象。

const REMOVE_ROUTE_POINT = (state: State, action: any) => {
  const droneRoute = {...state.entities[action.payload.routeId]}
  droneRoute.waypoints = droneRoute.waypoints.filter(
    wp => wp.id !== action.payload.waypoint.id); 

  return featureAdapter.updateOne({
    id: action.payload.routeId,
    changes: droneRoute,
  }, state);
};

const ADD_ROUTE_POINT = (state: State, action: any) => {
  const droneRoute = {...state.entities[action.payload.routeId]};
  droneRoute.waypoints = [...droneRoute.waypoints, ...action.payload.waypoint];

  return featureAdapter.updateOne({
    id: action.payload.routeId,
    changes: droneRoute,
  }, state);
};

关于angular - 更新 EntityAdapter 内的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56404714/

相关文章:

Angular 2如何防止订阅触发,除非 map 返回的两个值中的任何一个发生变化

angular - 为什么 Ngrx-Effects 不在继承的泛型类中自动订阅

javascript - typescript : Property 'data' dose not exist on type HTMLElement

angular - pKeyFIlter : NG8007: The property and event halves of the two-way binding 'ngModel' are not bound to the same target

Angular 7 Forms - 有条件地需要 FormGroup

angular - 这个 ngrx 选择器在做什么?

ngrx - @ngrx/Store 如何用于缓存?

Angular NGRX : Use withLatestFrom to call api only once. 但是, 'loader' 仍然设置为 true

html - 删除 Bootstrap 表中一个单元格的边框

javascript - 我如何检查 Angular 中的 Mobx 商店是否为空?