问题描述
我想对数据表列进行排序,但是它不起作用。下面是我得到的代码和错误。当我从打字稿给服务器调用后端时,它起作用了。当我使用状态管理时,排序不起作用。
<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;