0

Conventionally, an Angular/ngrx application maintains global state via NgRx's 'forRoot' at app.module.ts. Subsequently other sub states are maintained as 'features' within the global root state.

Given NgRx cli's schematic below one can apply root state at app.module.ts:

ng generate @ngrx/schematics:store State --root --module app.module.ts

However, this implies app.module.ts does NOT have to be root since it's an option.

QUESTION: Can we choose INSTEAD other.module.ts ( (lazy) loaded by app.module.ts) to store its own root state (and features for it's child modules only)?:

ng generate @ngrx/schematics:store State --root --module other.module.ts

In other words, can other.module.ts maintain its own 'root' state for its sub-module features? Note, this means there is NO app.module.ts-wide state (app.module.ts) but only other.module.ts-wide state.

MoMo
  • 1,836
  • 1
  • 21
  • 38
  • Are you expecting to be able to have multiple roots? Or just that one defined by that module? As long as there's only one, I don't think there's any restriction over what module defines the root. – Jeff Mercado Oct 14 '21 at 15:07
  • @JeffMercado Please assume one 'root' at other.module.ts for now. Not to pollute things but if this can be done it'shouldn't matter, right?. Basically one would have multiple 'root's at each module level. Which is questionable given feature states are best for that. – MoMo Oct 14 '21 at 15:11
  • You can have forFeature stores which lazy load. Checkout https://stackoverflow.com/a/61895189/13087977 – Deitsch Oct 14 '21 at 16:09
  • @Deitsch Thanks, but not my question. – MoMo Oct 14 '21 at 16:10
  • In my applications i have a separate Redux Module which is my root and gets added in the app.module.ts. I guess one could also add that in a submodule – Deitsch Oct 14 '21 at 16:16
  • In Redux, there is one object that holds state. In Flux, there are multiple objects. NgRx is Redux, therefore you can't have multiple root objects. – Brandon Taylor Oct 14 '21 at 16:48
  • @Brandon Thanks, but not my question. There's only one root but a different module. – MoMo Oct 14 '21 at 17:26
  • When using NgRx, modules adhere to the same principle. Any module other than the app module should use `forFeature` which establishes a key in the root state object. – Brandon Taylor Oct 14 '21 at 17:29
  • @Brandon, thanks. Again not my question. – MoMo Oct 14 '21 at 17:34
  • @Momo I'm not sure you understand what you're asking, but happy coding – Brandon Taylor Oct 14 '21 at 17:35
  • @Brandon, thanks. If you believe my question needs clarification please suggest where and I will gladly address. Otherwise read JeffMercado response. He understands the question. – MoMo Oct 14 '21 at 17:48
  • I read his comment, but I don't see why you would establish the root in any other module except the app.module – Brandon Taylor Oct 14 '21 at 17:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238149/discussion-between-momo-and-brandon). – MoMo Oct 14 '21 at 17:50
  • @Momo, no thanks, I have other matters to attend to, but it sounds like you've got the answer you're looking for. – Brandon Taylor Oct 14 '21 at 17:51

0 Answers0