无法分配给对象'[object Array] primeng只读属性'0'

问题描述

我想对数据表列进行排序,但是它不起作用。下面是我得到的代码和错误。当我从打字稿给服务器调用后端时,它起作用了。当我使用状态管理时,排序不起作用。

Error image

<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>

Data source

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;

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...