我正在使用Angular和NGXS在一个简单的应用程序上,以使用户可以创建带有标签的简单产品,但是我在更新嵌套属性方面却举步维艰。
我有一个User
模型和一个Product
数组。
export interface User {
id: number;
name: string;
products: Product[];
}
“产品”模型具有一组标签。
export interface Product {
id: number;
name: string;
tags: Tag[];
}
Tag模型也具有一些属性。
export interface Tag {
id: number;
name: string;
}
我希望能够添加,获取,更新,删除单个产品或单个标签。
在NGXS的GitHub文档(https://github.com/ngxs/store/blob/master/docs/recipes/immutability-helpers.md)上,我找到了以下示例,该示例更新了数组中的simgle属性:
export interface TrelloStateModel {
tasks: {
[taskId: string]: Task;
};
}
export class TrelloState {
@Action(UpdateDueDate)
updateDueDate(ctx: StateContext<TrelloStateModel>, action: UpdateDueDate) {
ctx.setState(
patch({
tasks: patch({
[action.taskId]: patch({
dates: patch({
dueDate: action.dueDate
})
})
})
})
);
}
}
但是我只是不知道如何使该模型适合我的对象模型。有更好的替代方法吗?