IAmTheNewOne
IAmTheNewOne

Reputation: 11

What is the actual use of a shared module in Angular?

I read on several Best Practices sides the same project structure for angular web applications. Examples are aglowildsolutions, tatvasoft or even the angular homepage. But I don't get the use of a shared module.

For instance, I use angular material design. That means I have about 10 material components. I have 5 feature modules and in each feature module I use 2 different material components. But to use them in these feature modules, I have to import the entire shared module into each feature module. Which means I still import everything the shared module offers. Isn't that practically the same as having everything just in the app module? Or the core module?

I dont see any advantage here. Its even the opposite. It makes the project structure much more crowded and inflates the amount of code which might even slow down the performance.

Upvotes: 1

Views: 324

Answers (1)

Zerotwelve
Zerotwelve

Reputation: 2361

In my opinion the shared module is an old practice for lazy programmers. This doesn't enable tree-shaking and leads to big bundle size.

My advice is to create on module for every single component. Import only the needed Material modules in each Component-Module.

Doing this your code will be more organized, enables good lazy loading and tree-shaking and also refactoring will be simpler

Upvotes: 1

Related Questions