问题
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