ngrx排序和更新状态内的对象中的数组
无法将排序的数组分配回状态。
分配给SubComponentData的接口类型IDATASET看起来像这样。排序已排序,但是如果我尝试将其分配给GriddataArray,则无效。我得到类型不匹配{} []不是[{}]。
export interface IDataSet {
gridDataArray: [
{
column1: string;
column2: string;
column3: string;
column4: string;
column5: string;
name: string;
}
];
urls: string[];
}
状态对象看起来像这样。
export interface IFeatureState {
subComponentData: IDataSet;
isSpinnerActivated: boolean;
}
分配我以下我拥有的小测试数据确实有效。但是,如果我尝试分配“ artrout”或“ arr”回去,则不会编译,我会遇到此错误。
这些类型之间的“ subcomponentdata.griddataarray”的类型是不兼容的。类型'{column1:string;列2:字符串;第3列:字符串;第4列:字符串;列5:字符串;名称:字符串; } []'不能分配给类型'[{column1:string;列2:字符串;第3列:字符串;第4列:字符串;列5:字符串;名称:字符串; }]''
on(sortGridData, (state: IFeatureState, { payload } ) => {
const sortByKey = key => (a, b) => a[key] > b[key] ? 1 : -1;
const arr = state.subComponentData.gridDataArray.slice();
const arrOut = arr.sort(sortByKey(payload));
return {
...state,
subComponentData: {
...state.subComponentData,
gridDataArray: arrOut
//TEST DATA THAT WORKS
// [{ column1:'Test', column2: 'Test', column3: 'Test',
// column4: 'Test', column5: 'Test', name: 'Test' }]
}
};
}),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
}
然后在还原器中解决了问题:
),
我非常接近,但应得出答案。
现在,我的位置只能朝一个方向发展,因此我将在下一个方向上努力。
https://bobbyhadz.com/ has-elements target-allows
RESOLVED
Then in the reducer just changed to this:
}),
I was very close but owe the answer to this.
Now my sort only goes in one direction so I will be working on that next.
https://bobbyhadz.com/blog/typescript-source-has-elements-but-target-allows