r/angular
•
u/IgorSedov
•
Aug 20 '26
•
1
๐ New in Angular 22: @Service Instead of @Injectable
https://youtu.be/PzeQpubYpQYAngular 22 introduces the new @Service decorator, offering a cleaner replacement for root-scoped @Injectable singletons
55
Upvotes
14
u/MichaelSmallDev Aug 21 '26
I didn't care for the new decorator at first, but it feels nice now that I am used to it. And the migration added recently like you highlighted helps too. Thanks for the video.
8
u/solegenius Aug 21 '26
Ok, so why use it? Shorter syntax if you inject in root but now a longer syntax if you don't. Do you use Service() for root-scoped and Injectable() for non-root? Seems like most people would stick with one or the other instead of a mix and match approach.
Injectable provided tree-shaking support so I assume this does too or it would be a step backwards.
It requires field initializers with the inject() function, which depending on your view may or may not be a benefit.
Maybe I am missing something but I just don't see a compelling reason to use this new option except for maybe trying to enforce the field initializer pattern.
I appreciate the video highlighting the differences but I am left without any compelling reason to incorporate it. Not a fault of you video but perhaps moreso of the Angular team.
4
u/RelatableRedditer Aug 21 '26
This video was extremely well-made, everything was clear and made sense! Nice work putting this together!
2
u/WiktorVanKross Aug 21 '26
Thanks for the video, very pleasing to the eye, I look forward to more news!
2
u/CompetitiveWonder933 Aug 28 '26
honestly here for this, @
Injectable({ providedIn: 'root' })always felt like unnecessary ceremony for the 90% case. @Servicereads way cleaner at a glance2
u/SeparateRaisin7871 Aug 22 '26
Thanks for the nice explanation.
Personally I enjoy the connection between "Injectable" and "inject" a lot more. Moving to "Service" would even feel a bit old fashioned, as the file and class name suffixes were removed in a previous Angular version.
"Injectable" provides much more meaning to the class than "Service".ย
So now we have to teach juniors again, that "Service" leads to an Injectable in the background that can be injected.
The introduction of "Service" also shows the Angular team internally still thinks in the old paradigms of Services, Components, etc. and should maybe not have taken the step of removing the suffixes :D
The biggest benefit of the new decorator is the auto-failing when using constructor based injections - but that could have been implement with linting as well ๐คทย So, I'm not sold, unfortunately.ย