4

我是 NgRx 的新手。在我的项目中,我们有 3 个级别的惰性模块。我希望商店的状态根据模块层次结构嵌套,但是当我写作时:storeModule.forFeature('child',reducer) 我得到的是一个平坦的状态切片而不是分层的状态。

例如,如果我有父模块和子模块。我将在父模块storeModule.forFeature('father',reducer) 和子模块中定义storeModure.forFeature('child',reducer)

状态的结果不会是

{ father { child {}}}

而是:

{ father {}, child {}}

有没有办法像惰性模块一样使状态分层?

4

1 回答 1

0

您实际上可以拥有延迟加载的状态切片。假设您有以下模块配置

app
  father
    child

并且您希望(如您所述)具有以下状态

{
  father: {
    child: {}
  }
}

您需要确保:

  • 你有一个路由器在AppModule里面和路由器FatherModule
  • FatherModule您在AppModules路由器中延迟加载(!)。
  • ChildModule您在FatherModules路由器中延迟加载(!)。
  • 导入中的配置是正确的:在ChildModule:StoreModule.forFeature('child', childreducers)FatherModule:中StoreModule.forFeature('father', fatherreducers)

例如,如果您导航到其中的组件,FatherModule它将加载该father功能。但不一定是child功能。仅当您导航到某些加载ChildModule.

您当前所做的是将两种forFeature方法放在同一个模块中。这没有任何意义,因为“功能”与模块直接相关。

于 2020-08-12T06:48:03.570 回答