Multiple stores in ngrx

时光怂恿深爱的人放手 提交于 2020-08-22 09:38:42

问题


I am writing an enterprise-scale application with Angular and ngrx. The intention is to use Flux and ngrx throughout. For reuse and separability we require (at least) two state stores that do not interact with each other. But we do need both stores to be active at the same time, and potentially accessed from the same components.

Ngrx seems to be predicated on the assumption that there will only ever be one Store at once. Is there an approach that will allow me to have multiple Store objects (templated of course with different State objects), and have them both loaded and active at the same time?

I'm aware that 'best practice' recommends combining the stores into one. That's not viable here unless there is an entirely novel approach.


回答1:


I'd suggest setting up two feature states. Here are the relevant docs: https://github.com/ngrx/platform/blob/master/docs/store/api.md#feature-module-state-composition

Though it isn't the same thing as having two separate stores it is the same for most practical purposes. The feature states are loaded when the module that imports StoreModule.forFeature('featureName', reducers) is loaded. You could do this lazy or eager. Each feature state will have access to root state so you can put common state on root state that both can access. Feature states should never reference each-other as they may not be loaded and that would negate the reason for having them.



来源:https://stackoverflow.com/questions/49409381/multiple-stores-in-ngrx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!