angular - TypeError:无法添加属性 newField,对象不可扩展
问题描述
我收到错误
TypeError:无法添加属性 newField,对象不可扩展
虽然添加任何新键或更改任何值都不会发生,但不知道出了什么问题。
dynamicFilter;
public ionViewWillEnter() {
this.commonservice.showLoading();
this.store.dispatch(activeBookingActions.getBookingFiltersRequest());
this.bookingFilterSubscription = this.store.pipe(select(selectActiveBookingData)).subscribe(response => {
this.commonservice.hideLoading();
if (response.bookingdata && response.bookingdata.filterlist && response.bookingdata.filterlist.data && (response.bookingdata.filterlist.data.action === 'bookingFilters')) {
this.commonservice.hideLoading();
this.filterList = response.bookingdata.filterlist.data.list;
this.sortingList = this.filterList.sorting;
this.dynamicFilter = [...this.filterList.filter];
console.log(this.dynamicFilter);
this.dynamicFilter.forEach((x) => {
if (x.design_type == 'checkbox') {
x.design_type = 'sadas'; // this line giving error
}
});
}
});
}
试过
https://stackoverflow.com/a/49022130/12917651
没有运气。
解决方案
我认为您从无法直接修改数据的商店或 NgRx 获取数据(ngrx-store-freeze)。
尝试创建数组中每个项目的对象的副本。
this.dynamicFilter = this.filterList.filter.map(data => ({ ...data }));
console.log(this.dynamicFilter);
this.dynamicFilter.forEach((x) => {
if (x.design_type == 'checkbox') {
x.design_type = 'sadas'; // this line giving error
}
});
Map 将不可变地转换数组,并且数组中的每个对象都使用扩展运算符 (...) 传播到内存中的新位置。
推荐阅读
- python-3.x - 如何隐藏/删除 mayavi 管道?
- twitter-bootstrap - 建议下拉菜单覆盖日期选择器
- python - django 和 sorl.thumbnail 模型字段的问题
- angular - Angular Universal,如何将带有声明的模块与服务器端执行隔离开来?
- php - 在服务器中访问远程mysql数据库laravel
- python - 检测键盘输入
- java - 验证组接口放在哪里?
- linux - xdg-mime 安装不更新文件 mime 类型关联
- javascript - 网页中的经典富文本格式
- python - 将 pandas df 导出到 sqlite 会导致重复的数据集而不是一个更新的数据集