gpt4 book ai didi

angular - 错误类型错误 : Cannot assign to read only property 'isSelected' of object '[object Object]'

转载 作者:行者123 更新时间:2023-12-04 00:21:50 26 4
gpt4 key购买 nike

我只想在克隆后更新我的 NgRx 存储对象中数组元素的属性值以避免突变但没有成功。
这是 reducer 代码:

on(
myActions.elementDeselected,
(state, { desiredId}) => {
const childArrayCopy=[...state.selectedObject.childArray.slice(0)];
const childArray = childArrayCopy.map(arrayElement=> {
if (arrayElement.id === desiredId) {
arrayElement.isSelected = false;
return arrayElement;
}
return arrayElement;
});

return {
...state,
selectedObject: {
...state.selectedObject,
...childArray
}
};
}
),

最佳答案

arrayElement是只读的,你也必须复制它:

const childArray = childArrayCopy.map(arrayElement=> {
if (arrayElement.id === desiredId) {
arrayElement = {...arrayElement, isSelected: false}; // <===
}
return arrayElement;
});

使用扩展的对象属性版本创建一个新对象,其中包含旧对象属性的副本,然后更新 isSelected成为 false .

关于angular - 错误类型错误 : Cannot assign to read only property 'isSelected' of object '[object Object]' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60035933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com