9

There is something I wonder about modules. I started Angular 2 a while ago, so i search too many topics but I couldn't find any satisfying answer yet.

When we creating angular 2 application, using modules for sure. Also use nested modules of course. How many nested modules that we can use ? Is this doable or is there a limit for nested modules ?

For example lets say i got admin dashboard application. Can we build like this

app.module.ts
|
 --dashboard
    |
    --dashboard.module.ts
      |
      --login
        |
        --login.module.ts
        .
        .
        .

We can struct with that way for sure. But lets say we have 5 or more nested module, is it ok for angular application? Can cause any problem or cause any performance problem ? Or we should keep it simple (max 3 nested etc.) for practice ?

Also how tsc is behaving when nested modules and components as long as it increases ?

For summarize what is the pros, cons nested modules etc. and what is best practice for nested module structuring ?

isherwood
  • 58,414
  • 16
  • 114
  • 157
orhun.begendi
  • 937
  • 3
  • 16
  • 31
  • That sounds wrong. Module is for separating things completely (e.g. internal dashboard vs public facing site). – Tatsuyuki Ishi Mar 26 '17 at 00:54
  • but on some point we're using modules side by side. There is so many project on github that using nested modules. just wonder why and what is the point of it ? – orhun.begendi Mar 26 '17 at 00:59
  • Nesting them is accepted and ran by the browser as normal; however, from the point of maintenance, you should make the tree more flat. Please edit your post to show a more detailed example of your nested matters. – Tatsuyuki Ishi Mar 26 '17 at 01:00
  • I think you mean component rather than module? – Michael Kang Mar 26 '17 at 01:03
  • No, i didn't meant to component. We can call modules from modules, if we use like this what is the effect for us? Is it just bad code or what ? – orhun.begendi Mar 26 '17 at 01:05
  • The angular style guide talks about "feature modules" and why to use. Maybe that's relevant here: https://angular.io/guide/styleguide#feature-modules – eflat Aug 30 '17 at 18:04

2 Answers2

19

Nested or not nested is not a black and white situation. Unfortunately, as for most of software development, "it depends."

However, I'd urge you to consider this - the point of the NgModule (besides the technical motivation of allowing AOT) is to provide a higher level 'unit' for your application. In other words, you can group individual components/services/pipes into discrete groupings that allow you to treat that grouping as a single unit that provides a certain amount of functionality. In most cases, this is used to provide features in your application (so called 'feature modules'), but the NgModule system is also used to provide other types of cross-cutting concerns. In fact, it becomes easy for library authors to distribute their library as a single NgModule, encapsulating all the functionality that they provide. (Examples include built-in libraries such as HttpModule and FormsModule, but also MaterialModule, FlexLayoutModule, etc.)

This use case of thinking of NgModule as a distribution container helps me think of how I should group my components/services/pipes - it's not always possible, but I try to think that I can take a folder containing the module definition and its various parts and should be able to drop that folder into any other application and it should basically work (assuming the presence of its external dependencies). Thinking of it this way helps to focus me on how granular to make the NgModule. This is not to say I don't nest folders within that NgModule, but just because there's a folder nested doesn't automatically mean I create a NgModule - unless the items make sense as a distribution container of some sort, I won't bother to create nested NgModules just to match the folder structure.

To summarize, your folder structure doesn't automatically mean you create NgModules for the deeply nested folders - and as such, there probably isn't a need for a deeply nested NgModule setup.

KB_
  • 2,113
  • 2
  • 26
  • 28
snorkpete
  • 14,278
  • 3
  • 40
  • 57
  • I'm working on a large shared module and I'm trying to break some of the components down into modules based on their shared functionality (e.g. modal-like components). My issue is importing my "sub"-module into shared. Now it no longer has access to other declarables in shared (e.g. a custom pipe). Are you saying even though this functionality can be grouped in a folder, it is best not to create a "sub"-module, because theoretically you couldn't take the module and let it stand alone in another project? (i.e. it would need those other dependencies like the custom pipe which it would not have) – Esten Feb 10 '20 at 21:22
  • I don't completely understand the context surrounding your question, but if i am getting the gist right, you technically can't create the submodule you're trying to create, since it (the potential sub module) won't have access to (at least one) custom pipe that it needs. But, my point was that you can go ahead and use folder structures that make sense from a code organization perspective, but don't feel compelled to create submodules solely because you put your code into sub folders . – snorkpete Feb 12 '20 at 04:10
3

For me, the main goal of using NgModules 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!).

Hossam El-Deen
  • 972
  • 1
  • 13
  • 27