0

我正在使用带有 Redux 的 Angular 2+ 并且很好,但不能用新值修改数量

错误是:TypeError:无法分配给对象“[object Object]”的只读属性“数量”

export interface CartState {
    readonly cars: CartCarModel[]
  }

  export class CartCarModel {
    constructor (
      public carId: string,
      public carModel: string,
      public quantity: number,
      public price: number) { }
  }


  const initialState: CartState = {
    cars: []
  }

  function reducerFunc (state: CartState, id: string, quantity: number) {
    const nextState = produce(state, draft => {
    draft.cars.find(d => d.carId === id).quantity = quantity
  })

  return Object.assign({}, state, {
    drinks: nextState
  })
}
4

1 回答 1

0

因为在redux中你不能修改状态的值你必须复制值所以使用map 而不是find 我希望事情进展顺利

于 2021-02-19T12:57:05.083 回答