ngrx中的多个商店 [英] Multiple stores in ngrx

查看:80
本文介绍了ngrx中的多个商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Angular和ngrx编写企业级应用程序.目的是在整个过程中使用Flux和ngrx.对于重用和可分离性,我们需要(至少)两个彼此不交互的状态存储.但是我们确实需要两个商店都同时处于活动状态,并可能从相同的组件进行访问.

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似乎是基于一个假设,即一次将只有一个Store.有没有一种方法可以让我拥有多个Store对象(当然,这些对象具有不同的State对象作为模板),并且可以同时加载和激活它们?

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.

推荐答案

我建议设置两个要素状态.以下是相关文档: https://github.com/ngrx/platform/blob/master/docs/store/api.md#feature-module-state-composition

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

尽管这与拥有两个单独的存储库不同,但对于大多数实际用途而言都是相同的.加载导入StoreModule.forFeature('featureName', reducers)的模块时,将加载功能状态.您可以这样做懒惰或渴望.每个功能部件状态都可以访问根状态,因此您可以将通用状态放在两个都可以访问的根状态上.功能状态永远不要互相引用,因为它们可能不会被加载,这会否定拥有它们的原因.

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.

这篇关于ngrx中的多个商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆