Angular2/Typescript/ngRx - TypeError:无法分配给对象的只读属性

vik*_*iga 6 javascript typescript redux ngrx angular

在构造函数中,我做了类似的事情

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

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

this.selectedDate['date'] = new Date(this.selectedDate['pristine']);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

TypeError:无法分配给对象'[object Object]'的只读属性'date'

vik*_*iga 6

致Ryan Q的信用:

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

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

this.store.dispatch(new settime.DateChangeAction(Object.assign({}, this.selectedDate)));
Run Code Online (Sandbox Code Playgroud)