Modules: when and why? [closed]

Ultimately the module concept came to Angular 2 because of the need for lazy loading. There needed to be a single place where the dependencies of a section of one’s application could be declared and services provided.

As a result, my preference in general is to only divide the application into modules as is necessary to enable lazy loading. Personally I find any more module declarations of limited use.

Still, there is no single best practice as far as modules are concerned. Much of it is project-dependent. Some developers prefer to create a module for every component, while others have one single module for their entire application.

In favor of using large numbers of modules: there are few drawbacks to creating those modules other than code verbosity. You will have smaller sections of your application that can be rearranged and moved into other applications with ease. Dependencies for specific areas of your application are also more explicit (rather than having all of your application’s directives available to all components).

In favor of using small numbers of modules: you will spend less time trying to declare the components that are shared between modules. A single root module containing all of your component declarations is the single source of truth for your application’s dependencies.

In general, I would say go with your gut. Choosing to create a module is not so different from choosing to create a new folder in your application. If you find that you’re uncomfortable with the size and scope of a module, the cost of refactoring is minimal.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)