我正在使用 @ngrx/entity
这将创建一个如下所示的状态:
{
ids: [1, 2, 3],
entities: [
1: {id:1, title: "Some title", likes: {count: 1}},
2: {id:2, title: "Some title", likes: {count: 1}},
3: {id:3, title: "Some title", likes: {count: 1}}
]
}
@ngrx/entity
确实为我们提供了一些更新项目的酷 helper ,但似乎(从我在文档中看到的)仅限于更新整个实体。但是,当用户切换“喜欢”按钮时,我希望在我的 reducer 中仅更新
state.entities[2].likes
属性与响应。关于如何解决这个问题的任何想法?
最佳答案
因为你的状态是不可变的。您需要更新所有实体。 @ngrx/entity 附带了一些可用于更新 1 个实体的帮助程序。在您的情况下,您需要使用 updateOne 方法。 https://ngrx.io/guide/entity/adapter
它看起来像这样:
adapter.updateOne(
{
id: 2,
changes: {...state.entities[2], likes: value}
},
state
);
关于angular - 使用@ngrx/entity,如何更新项目数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55971650/