gpt4 book ai didi

javascript - Angular2/Typescript/ngRx - 类型错误 : Cannot assign to read only property of object

转载 作者:数据小太阳 更新时间:2023-10-29 05:11:38 25 4
gpt4 key购买 nike

在构造函数中我做了这样的事情

selectedDate: Object;
// construtor
this.selectedDate = {};
this.selectedDate['date'] = new Date();
this.selectedDate['pristine'] = new Date();

在另一个通过单击按钮调用的函数中,我执行以下操作:

this.selectedDate['date'] = new Date(this.selectedDate['pristine']);

我收到以下错误:

TypeError: Cannot assign to read only property 'date' of object '[object Object]'

最佳答案

感谢 Ryan Q:

正如 Ryan 评论的那样,我在我的应用程序中使用了 ngRx,并且我确实通过操作将 this.selectedDate 对象传递到商店中,因此我无法修改存储在状态中的对象。

解决这个问题的一种方法是传递一个新的对象引用,这应该可以解决这个问题:

this.store.dispatch(new settime.DateChangeAction(Object.assign({}, this.selectedDate)));

关于javascript - Angular2/Typescript/ngRx - 类型错误 : Cannot assign to read only property of object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44785645/

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