如何解决无法分配给对象'[object Array] primeng只读属性'0'
我想对数据表列进行排序,但是它不起作用。下面是我得到的代码和错误。当我从打字稿给服务器调用后端时,它起作用了。当我使用状态管理时,排序不起作用。
<th *ngFor="let col of columns" [pSortableColumn]="col.field" [ngSwitch]="col.field">
{{col.header}}
<p-sortIcon [field]="col.field" ariaLabel="Activate to sort"
ariaLabelDesc="Activate to sort in descending order" ariaLabelAsc="Activate to sort in ascending order"></p-sortIcon>
</th>
device.action
export class LoadDeviceFail implements Action {
readonly type = DeviceMgtActionTypes.LOAD_DEVICE_FAIL;
constructor(public payload: string) { }
}
export class LoadDevices implements Action {
readonly type = DeviceMgtActionTypes.LOAD_DEVICES;
constructor() { }
}
export class LoadDevicesSuccess implements Action {
readonly type = DeviceMgtActionTypes.LOAD_DEVICES_SUCCESS;
constructor(public payload: DeviceModel[]) { }
}
device.effect
@Effect()
loadDevices$ = this.action$.pipe(
ofType(
deviceMgtActions.DeviceMgtActionTypes.LOAD_DEVICES
),switchMap((action: deviceMgtActions.LoadDevices) => this.restApiService.getAll('/iot/Devices/AllDevices').pipe(
mergeMap((devices: DeviceModel[]) => [
new deviceMgtActions.LoadDevicesSuccess(devices),new deviceMgtActions.ShowLoader(false),]),catchError((error) =>
of(
new deviceMgtActions.LoadDevicesFail(error),new deviceMgtActions.ShowLoader(false)
)
)
)
)
);
device.reducer
switch (action.type) {
case DeviceMgtActionTypes.LOAD_DEVICES_SUCCESS:
return {
...state,devices: action.payload,error: '',};
case DeviceMgtActionTypes.LOAD_DEVICES_FAIL:
return {
...state,devices: [],error: action.payload,};
devices.ts
getDevices(): void {
this.store.dispatch(new deviceMgtActions.ShowLoader(true));
this.store.dispatch(new deviceMgtActions.LoadDevices());
}
构造函数
constructor(private store: Store<fromDeviceMgt.State>)
this.store.pipe(select(fromDeviceMgt.getDevices),takeWhile(() => this.componentActive))
.subscribe((devices: DeviceModel[]) => {
this.devices = devices;});
}
解决方法
我找到了答案。 在构造函数中,当您收听getdevices时,必须使用cloneDeep来创建值的深层副本。
this.store
.pipe(
select(fromDeviceMgt.getDevices),takeWhile(() => this.componentActive)
)
.subscribe((devicesLoaded: DeviceModel[]) => {
this.devices = cloneDeep(devicesLoaded);
});
您必须从'lodash / cloneDeep'导入cloneDeep;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。