简体   繁体   English

我们可以在 ngrx 中将实体注册到全球商店吗? 那可能吗?

[英]can we register entity to global store in ngrx? is that possible?

It would be nice if we entity in our global store?如果我们在我们的全球商店实体,那会很好吗?

I have some crud operations, which are very easily done using @ngrx/entity and I want to have it in root-store.我有一些 crud 操作,使用 @ngrx/entity 很容易完成,我想把它放在根存储中。

I have tried creating an entity, but schematics did it for forFeature().我试过创建一个实体,但原理图是为 forFeature() 做的。

NgRx Entity is basically a Dictionary with matching NgRx operators to update/read. NgRx 实体基本上是一个字典,具有匹配的 NgRx 运算符以更新/读取。
You can always hold an Entity Object in any store that can store a string for example.例如,您始终可以在任何可以存储字符串的商店中持有实体 Object。 You can do so by extending the Store if you just need one Entity Object如果您只需要一个实体 Object,则可以通过扩展商店来实现

export interface State extends EntityState<User> {
    ...
}

or you can create Entity Objects in the Store like或者您可以在商店中创建实体对象,例如

export interface State {
  users: EntityState<User>;
  books: EntityState<Book>;
}

Both ways you can work with adapters.您可以通过两种方式使用适配器。 For the extend version pass the whole State to the adapter.对于扩展版本,将整个State传递给适配器。 For the second approach pass the users property to the adapter method and set the result as new value for that property in the state.对于第二种方法,将用户属性传递给适配器方法并将结果设置为 state 中该属性的新值。

Edit编辑
To update the Entity object a adapter is used.要更新实体 object,使用适配器。 Create an adapter and use it in the reducers.创建一个适配器并在减速器中使用它。 A lot more infos about it and code examples can be found at https://ngrx.io/guide/entity/adapter有关它的更多信息和代码示例可以在https://ngrx.io/guide/entity/adapter找到

// adapter definition
export const userAdapter: EntityAdapter<User> = createEntityAdapter<User>();

// reducer call for extended state / first version
on(UserActions.setUsers, (state, { users }): State => userAdapter.setAll(users, state)),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM