Is it better to have one module for each component in Angular, or to work without costume modules (I mean component module); I'm beginner; And before, I worked without a module for each component (I mean, I was creating a component and passing it to declarations in app.module.ts
), but this week I found out a tutorial, where the guy was creating a module for each component. Any tips or suggestions please
CodePudding user response:
I think I vsn tell you what I do, I follow this protocol for modules and my apps,
Each feature - individual modules (let us say you have a landing page, customer details page, about us page so each of these page is made up of multiple components and each page will have its own functionality, I design one module for each of these feature modules.)
External library dependency - one module (Lets us assume you are using material library, to manage its dependencies in main app I would use one module, you can find its reference in material library documentation also.)
Shared features - one module, ( If you have some shared logic such as pipes, common logic, app wide common service I design a shared module for such features)
Think as a group - individual module for each, ( Whatever you do in angular and if you can group them together as a group which is logical try to think for a module.)
Note: Don't create unnecessary modules for each task, only make them such that it gets easier for you to manage
Do read this document for more details, Angular style guide
CodePudding user response:
Based on my experience, the best approach is to create a module for each component.
That
- Simplify your module imports in unit test
- Lazy loading for the win
- Separate the concerns
- Much easier refactor
- ...