NGRX如何将多个特性模块嵌套在彼此内部?

3

目前我使用的商店是由特性模块组成的平面结构,即:

{
    feature_A: {
       A:'A',
       B:'B'
    },
    feature_B:{
        C:'C',
        D:'D'
    }
}

但是我想要做的是:

{
    feature_A: {
       A:'A',
       B:'B',
       feature_B:{
        C:'C',
        D:'D'
      }
    }
}

也许像这样:App.Module
StoreModule.forFeature('feature_A', fromFeature_A.feature_A_Reducers)
StoreModule.forFeature('feature_A.feature_B', fromFeature_B.feature_B_Reducers)

在网上搜索了这个问题,没有找到真正的答案,只是推荐保持特性模块的平坦结构。

1个回答

6
ngrx中的特性模块仅是根对象的直接状态,因此并没有嵌套特性模块的概念。
相反,您可以通过将特性B的状态抽象到不同的文件(reducer、action、effect等)中,并将其作为特性A的子状态来实现类似的行为。
然后,您可以创建一个选择器来访问特定的特性B状态。
const selectFeatureA = createFeatureSelector('feature_A');
const selectFeatureB = createSelector(selectFeatureA, (state) => state.feature_B);

1
你是对的...如果有一种方法,那就是这种方法...但我们决定采用扁平存储结构而不是嵌套对象。 - Tomas Katz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接