Angular 在NgRx实体中更新字典
我正在尝试更新,或者更确切地说是在ngrx实体下的字典中插入新值 以下是实体的简化视图:Angular 在NgRx实体中更新字典,angular,typescript,ngrx,ngrx-entity,Angular,Typescript,Ngrx,Ngrx Entity,我正在尝试更新,或者更确切地说是在ngrx实体下的字典中插入新值 以下是实体的简化视图: interface Dictionary<T> { [key: number]: T } export class Item { id: number; partsDescriptions: Dictionary<string[]>; } export interface ItemsState extends EntityState<Item> { } 在r
interface Dictionary<T> { [key: number]: T }
export class Item {
id: number;
partsDescriptions: Dictionary<string[]>;
}
export interface ItemsState extends EntityState<Item> {
}
在reducer中,我尝试执行以下操作:
on(MyActions.partDescriptionsLoaded, (state, action) => {
return {
...state,
items: adapter.updateOne({
id: action.id,
changes: {
...state.items[action.id],
partsDescriptions: {
...(state.items[action.id] && state.items[action.id].partsDescriptions),
[action.partId]: action.partDescriptions
}
}
}, state.items)
}
})
PartDescriptions字典没有扩展,它总是被替换。你知道如何解决这个问题吗?作为一般回答:假设一个对象处于状态,比如说
descriptions
,为了更新它而不是替换它,您将执行以下操作:
说明:{
…状态描述,
新的描述,
}
对于您的代码,请尝试执行以下操作:
零件说明:{
…state.parts说明,
元素您希望使用扩展此对象,
}
on(MyActions.partDescriptionsLoaded, (state, action) => {
return {
...state,
items: adapter.updateOne({
id: action.id,
changes: {
...state.items[action.id],
partsDescriptions: {
...(state.items[action.id] && state.items[action.id].partsDescriptions),
[action.partId]: action.partDescriptions
}
}
}, state.items)
}
})