For me, the main goal of using NgModule
s at all is lazy-loading; I'm not a fan of logical/feature structuring in angular. If there was no lazy-loading in angular, I'd probably just structure the code this way:
components/
services/
pipes/
models/
...
Lazy-loading-wise, nesting modules vs not nesting them makes no difference. And I probably decide which code is loaded together by which code is on the same page. For instance, even if 2 components feel like they belong to the same feature but they're on different pages, I'd want them to be on their respective pages' modules for better lazy-loading.
So, I currently structure my code this way:
app
|
--services/
|
-- the services module called `CoreModule` in angular docs.
--shared/
|
-- the shared module described in angular docs. Has models as well.
--pages/
|
-- page1/
|
-- a module for page1/feature1 that I'll use in lazy-loaded manner (but may not do so if found unsuitable).
-- page2/
|
-- a module for page2/feature2 that ...
So, answering your question in specific about nesting:
- Nesting modules is actually just a folder structure; there's no effect vs non-testing angular-wise. A nested module is just like a top-level module. E.g., you can import
login.module.ts
in a module beside dashboard
.
- Since it's a folder structure, there's no limit on the levels of nesting angular-wise.
- However, usually in nested modules, you have a module X importing a module Y importing a module Z, ... etc. For this point, I don't if it has effect on performance or no but I don't expect it to have. Especially that the same situation exists in library dependencies: imaging using an angular component/module from some npm package that uses another angular component/module in another npm package, ... etc.
- For my opinion on nesting modules & code structure in general, I've outlined it above.
As a source for avoiding nesting, see "Do keep a flat folder structure as long as possible" principle in Angular's official Style Guide. In general, the Style Guide seems a good place for similar questions (just discovered it!).