ismaestro
ismaestro

Reputation: 8267

Best Practices: Standalone Components vs. Modules in Angular 14

I'm seeking clarification on the best practices regarding the usage of standalone components and modules in Angular 14. Given the introduction of standalone components as a new concept in Angular, I'm looking for factual guidance supported by citations or references.

I've created a new whole app only using standalone components with lazy loading routes, and everything works perfectly. Here the repo: https://github.com/Ismaestro/angular-example-app

However, I'm curious if this indicates a potential shift towards standalone components rendering NgModules obsolete.

Could someone provide evidence-based insights into the advantages and disadvantages of employing standalone components versus modules in Angular 14?

Thank you for your contributions.

Upvotes: 63

Views: 49082

Answers (2)

Caricard
Caricard

Reputation: 391

For practical upsides/downsides of using Standalone vs Modules check out this discussion on Reddit: https://www.reddit.com/r/Angular2/comments/118i0pf/do_standalone_components_cause_duplicate_code_in/

The answer is not so easy and straightforward as some debaters suggest. I'm not going to repeat many advantages of standalone - they are stated everywhere, but the main omitted disadvantage of using only standalone components seems that you might end up loading the same code many times as described within the discussion. I will also borrow the conclusion from there:

"So I guess it becomes a balancing act of including too much in main.js vs having things duplicated in multiple lazy-loaded files? Like maybe if something's really needed everywhere, I would keep it in Shared but if it's only needed by some modules it might be better to make it standalone?" R00B0T

Upvotes: 5

MoxxiManagarm
MoxxiManagarm

Reputation: 9134

Standalone components are not mandatory and will never be, there is no rule when to use them. However, Angular Architects recommend to always use Standalone components at least for new components you create. They are simply more treeshakeable and less boiler. You can mix standalone components and modules also.

For the mentioned recommendation of the Angular Architects, you can also watch the Webinar from last Monday: https://www.youtube.com/watch?v=9rj8kR0q0c8

Upvotes: 45

Related Questions