Scipion
Scipion

Reputation: 11888

Angular2 modules: best practices

I have always been working with one main module called AppModule but I am yet to divide any of my small apps in several modules. What are the best practises regarding Modules ? In other terms, when should I refactor my code to encapsulate some functionalities in a Module ?

Any examples that could help are very welcome.

Cheers

Upvotes: 3

Views: 1520

Answers (2)

Rap
Rap

Reputation: 7292

The main use case for modules is ... well ... modularity. It is the chief way for Angular to implement SRP and SOC. It creates good designs for our Angular apps by increasing cohesion and reduces coupling.

There are other reasons which John Papa lists here:

It makes sense that some of these features have functionality that is not intended to be exposed outside of the feature. We also may want to add a feature to the app as a whole unit at a later time. Or maybe the feature is there and we want to lazy load it when the user decides it is time to visit the feature.

These are all valuable, but the main purpose for modules is good design.

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657346

The main use case for modules is lazy loading, there move components, ... you want to lazy load into a module.

Besides that, usually modules are split to have a reusable feature per module.

Also modules can export other modules and components, ... from other modules, to make a more complex feature out of several modules.

Upvotes: 2

Related Questions