34

I would like to know when should I use Standalone components and when should I create a new module. Since it's a new concept introduced in Angular, what are the criteria to use them?

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

So this means NgModules are obsolete?

What are the advantages and disadvantages of each of them?

Thanks in advance.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
ismaestro
  • 7,561
  • 8
  • 37
  • 50

1 Answers1

22

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

MoxxiManagarm
  • 8,735
  • 3
  • 14
  • 43
  • 1
    Nice!, so I guess if I start a new application is better to use only standalone components? Thanks for the link – ismaestro Nov 24 '22 at 15:22
  • Yes, this approach is recommended. That is the way Angular is going forward. – MoxxiManagarm Nov 25 '22 at 08:47
  • 3
    I'm playing with standalone components right now and I agree that we should all use standalone components over modules (at least going forward). That said, I think a potentially valid reason to use a module would be if you _know for a fact_ that some number of components you are writing will _always_ be used _together_. Then I would still write them as standalone components, but you can easily create a module to import and export all of the components so they are easy to import as one unit. This is just and idea I'm playing with and would love to here other thoughts. – Troy Weber Dec 30 '22 at 01:17
  • 1
    I think thats a fair use of ng modules!. I've created a post on dev.to and an example app. Here the links: https://dev.to/ismaestro/implementing-standalone-components-in-angular-15-3c2p and https://github.com/Ismaestro/angular-example-app – ismaestro Jan 18 '23 at 10:23
  • 2
    currently i use modules for the sake of grouping components and services for a specific functionality. for example i need something that is made up of 3 or more components and has some services that are suitable only for that single functionality , i group them in a single module . but for simple functionalities or the sake of sharing code and preventing unnecessary repeated code, standalone components are amazing! and they have no limitations, for your new projects you can only use standalone components with no problems – PayamB. Feb 08 '23 at 11:28
  • @PayamB. Hi, is it possible to actually use a standalone component to do the grouping of components and services for a specific functionality, instead of using module. If possible, wonder what is the advantage of using module over standalone in such case. – Nick Wills Mar 07 '23 at 16:54
  • "Standalone components are not mandatory and will never be". So does that mean module will NEVER be deprecated and removed in future? Cos if the day come when module is really removed, then standalone components will have to be mandatory. – Nick Wills Mar 07 '23 at 16:55
  • That is what they said, there is no plan to remove modules. – MoxxiManagarm Mar 07 '23 at 17:41
  • So is there any, and I mean ANY reason to NOT to use standalone coponents, ever? Where would we declare root paths, or an NgRx root? – pax Mar 23 '23 at 15:56
  • I total agree with @PayamB. NgModule and StandaloneComponent should used flexibal case by case. – vub May 16 '23 at 01:50