NGRX8减速器错误消息:类型'number'不能分配给类型'ListItem []'

时间:2019-12-27 18:59:47

标签: angular ngrx ngrx-reducers

我正在尝试设置NGRX以更新一系列列表项。

这是我的减速器

export const initialState: Array<ListItem> = [
  {name: 'list item', quantity: 1}
  ];

// tslint:disable-next-line:variable-name
const _listReducer = createReducer(
  initialState,
  on(add, (state, {name, quantity}) => (state.push({name: name, quantity: quantity})))
);

export function listReducer(state: ListItem[] | undefined, action: Action) {
  return _listReducer(state, action);
}

以及相关的动作

export const add = createAction(
  '[List Component] Add',
  props<{name: string; quantity: number}>()
  );

我的问题是如何正确地将新条目从化简器推入状态数组?当前,我收到此错误

ERROR in src/app/reducers/list.reducer.ts(12,40): error TS2322: Type 'number' is not assignable to type 'ListItem[]'.

1 个答案:

答案 0 :(得分:0)

'on'语句应该看起来像这样

  on(add, (state, {name, quantity}) => ([...state, {name, quantity}]))