typescript - 类型错误:“值”是只读的
问题描述
我正在尝试使用 ngrx 存储将数据发送到我的后端,但出现此错误:ERROR TypeError: "value" is read-only。我尝试了很多东西,但没有一个奏效。
我必须发送包含刚刚修改的属性数组的“设备”对象,相关设备已在商店中注册,我直接从组件中的商店获取它以制作我的表格
我的效果代码:
updateEquipment$ = createEffect(() =>
this.actions$.pipe(
ofType(SettingsActions.updateEquipment),
switchMap(action => {
const equipment = { ...action.equipment };
// const result = { ...action.gasxProperty };
action.gasxProperty.forEach(res => {
equipment.properties.find(prop => prop.name === res.name).value = res.value; // <== here i get the error
});
return this.settingsService.saveEquipment(action.equipment).pipe(
map(equipmentSaved => {
this.store.dispatch(CoreActions.hideSpinner());
this.toastrService.success('', 'Equipement sauvegardée.');
return SettingsActions.updateEquipmentSuccess({ equipment: equipmentSaved });
}),
catchError(() => {
this.store.dispatch(CoreActions.hideSpinner());
this.toastrService.error('', 'Erreur de sauvegarde.');
return of(SettingsActions.updateEquipmentError());
})
);
})
)
);
在我的组件上,我这样做是为了保存:
public equipment$: Observable<Equipment>;
constructor() {
this.equipment$ = this.store.select(fromSettings.getSelectedEquipment);
}
public onSubmit(equipment: Equipment): void {
const result = Object.keys(this.form.controls.gasx.value).map(key => ({
name: key,
value: this.form.controls.gasx.value[key]
}));
result.pop();
this.store.dispatch(CoreActions.showSpinner());
this.store.dispatch(SettingsActions.updateEquipment({ equipment, gasxProperty: result }));
}
的HTML:
<ng-container *ngIf="(equipment$ | async) as equipment">
<div *ngIf="!isEditable"
(click)="turnOnCheckbox(equipment)"
class="flex items-center justify-center w-8 h-8 rounded-full bg-smg-lightGray">
<img class="w-3 cursor-pointer"
src="../../../../../../assets/icons/pen.svg" />
</div>
</ng-container>
行动是:
export const updateEquipment = createAction(
'[Settings] Update equipment',
props<{ equipment: Equipment; gasxProperty }>()
);
有任何想法吗?
解决方案
您不能这样做,因为该操作已“冻结”(只读)。
action.gasxProperty.forEach(res => {
equipment.properties.find(prop => prop.name === res.name).value = res.value; // <== here i get the error
});
相反,您应该以不可变的方式更新它(使用扩展语法)
推荐阅读
- parquet - 在 Apache Beam 中使用 Snappy 压缩编写 parquet 文件
- python - Performantly argsort two pre-sorted numpy arrays
- sql - sql server 2008 r2 - 多列,每列都有因变量的总和
- css - Bootstrap vue / Bootstrap 侧导航可切换
- arrays - 接收条件推送数组的重复结果
- ruby-on-rails - 我收到一个错误 `to_s': wrong number of arguments (given 1, expected 0) and how can imrpove my "code"?
- python - 用于在 whatsapp 上向某人发送音频的 Python 脚本
- c# - 文本文件包含记录之间的回车
- react-native - 进程以非零代码退出:1 在创建 React Native App 期间使用 expo init 时出现子进程错误
- javascript - 从对象中删除元素 - javascript