我正在使用ngrx商店。
在我的状态下,我必须元素
export interface ISchedulesState {
schedulings: ISchedules;
actualTrips: ISchedule[];
}
这是我的界面
export interface ISchedules {
[key: string]: ISchedule[];
}
export interface ISchedule {
dest: number;
data: string
}
在 reducer 中,我更新了
actualTrips
export const SchedulingReducers = (
state = initialSchedulingState,
action: SchedulesAction
): ISchedulesState => {
switch (action.type) {
case ESchedulesActions.GetSchedulesByDate: {
return {
...state
};
}
case ESchedulesActions.GetSchedulesByDateSuccess: {
return {
...state,
schedulings: action.payload
};
}
case ESchedulesActions.GetSchedulesByTime: {
let time = action.payload;
state.actualTrips = [...(state.schedulings[time] || [])]; // if not data return empty array
return state;
}
default:
return state;
}
};
但实际上我得到一个错误
ERROR TypeError: Cannot assign to read only property 'actualTrips' of object '[object Object]'
最佳答案
Redux 模式的基本原则是状态及其部分的不变性,因为它让我们只通过对象引用来检测变化,而不是比较整个对象。
在您的 reducer 中,您不能直接分配状态属性( state.actualTrips =
),因为更改检测器(和选择器)不会将其检测为已更改。
要修改状态,请返回带有新修改的状态副本。
const time = action.payload;
return {
...state,
actualTrips: [...(state.schedulings[time] || [])]
}
关于javascript - Ngrx : Cannot assign to read only property 'Property' of object '[Object]' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57591012/