Why I decided to switch to the inject() function in Angular

Why I decided to switch to the inject() function in Angular

Joshua Morony

1 год назад

51,299 Просмотров

My modern Angular course: https://angularstart.com/

Angular 14 gave us the ability to use the inject() function more generally in our projects, including replacing constructor based dependency injection. But is it a good idea to switch to it?

More on Dependency Injection: https://youtu.be/ZzC-hfYQXvQ

Get weekly content and tips exclusive to my newsletter: https://mobirony.ck.page/4a331b9076

Learn Angular with Ionic: https://ionicstart.com

#angular

- More tutorials: https://eliteionic.com
- Follow me on Twitter: https://twitter.com/joshuamorony

0:00 Introduction
0:54 inject() benefits
1:53 Inheritance
4:05 The problem with inject()
5:36 Conclusion

Тэги:

#ionic #tutorial #tutorials #ionic_tutorials #ionic_framework #coding #coding_tutorials #angular #angular_tutorials #capacitor_tutorials #mobile #mobile_apps #apps #html5 #ios #android #native #hybrid #pwa #web_apps #progressive_web_applications #programming #stenciljs #stencil #performance #ui #ux #animations #screencast #rxjs #nx #ngrx #architecture
Ссылки и html тэги не поддерживаются


Комментарии:

mysty ani
mysty ani - 10.11.2023 11:55

for the longest time i used the constructor DI and inheritance but i realized sooner than later with new requirements it makes a hell of editing cycle so i switched to the inject() but using some backdoor way that allows injecting in ngoninit and so on by making custom AppInjector that gets injected in AppModule, that way it will be available to use whenever you need instead of constructor context too.
I don't understand perfectly why angular team didn't include this approach to be in their default injector, mostly something that has to do with lazy loaded modules/services and TreeShaking builds yet i find it very solid for most use cases.

SO in short HELL YES I AM WITH THE inject(service); to hell with constructor injection unless if you are forced to for some reason.

Or to be reasonable a mix and match might make sense in some cases when you need to make sure that who ever is extending your class knows it's a must to have these services. lot's of other frameworks stayed on the constructor injection and i do believe it's one of the reasons for that.

Ответить
Mehdi Mahmud
Mehdi Mahmud - 08.11.2023 14:52

thank you for th calrification. Just one thing. how do we use the services inside spec for testing?

Ответить
Magnus Kramnik
Magnus Kramnik - 25.10.2023 14:21

A better option would be to use annotations. Like @Autowired in spring.

Ответить
btx
btx - 07.10.2023 06:11

Omg you just solved my over years issue with components inheritance 😬 i tended to create nested sub properties like _ _ _ appService to get it somehow working cause I could not find any other alternative

Ответить
T Michael
T Michael - 06.09.2023 19:08

Makes usage with the occasional, unavoidable inheritance much nicer.

Ответить
Aleksandra Setsumei
Aleksandra Setsumei - 17.08.2023 10:27

huhu I've got a question and would love if someone could answer. So I do prefer using the inject function, but I run into the following problem: I mostly want to have the injected Services private as there is no need for accessing them outside of the component. And I mostly want to use the declerative approach and make my members constants. So it would look like this:

public readonly someObservable$ = someService.doSomething$();
private readonly someService = inject(someService);

this code obviously fails as I use the service before I declare it. But if I move the service up, then I have the problem with eslint and member ordering; it says that private members are to be declared after the public ones. I wonder, how can we fix this problem? I can of course just ignore the tslint rule for this case but is this the best practice?

Ответить
Clash Clan
Clash Clan - 06.08.2023 12:48

That adding removing in baseclass could break on runtime than compile time like constructor injection

Ответить
MerlinW
MerlinW - 26.07.2023 20:31

If you don't need the dependencies / different instances, use abstract class.

Ответить
Jay Bee
Jay Bee - 23.07.2023 20:42

What about for unit testing?

Ответить
BloodWoork
BloodWoork - 16.07.2023 12:11

I switched to inject and I love it, no regrets at all! :)

Ответить
Martin Nielsen
Martin Nielsen - 08.07.2023 11:04

How about 'private' constructor injections? Can the inject() approach replace the those? Like. Is a private variable deceleration for an inject(service) as private as when declared private in a constructor injection?

Ответить
ratg97
ratg97 - 08.07.2023 10:52

and testing? is more more complicated

Ответить
Ali Çelebi
Ali Çelebi - 06.07.2023 16:13

Great video, as usual :)

Ответить
Antonio Pekeljevic
Antonio Pekeljevic - 30.06.2023 18:11

This is so neat. I don't know why Angular doesn't mention it as an option in the Dependency Injection documentation page.

Ответить
aravind muthu
aravind muthu - 27.06.2023 07:23

Ha, really eager to see your faces when you find out in React, there is no such thing as injection or constructors. You can import anything from anywhere and just have it working.
Through the comments I see many people view an inject function as a Biblical sin and is a path to damnation. Grow up boomers, it is 2023 lol

Ответить
Dayron Alfaro
Dayron Alfaro - 18.06.2023 05:53

Yes I will Switch jeje is more simple

Ответить
titusfx
titusfx - 03.06.2023 15:11

The video started saying that is better for juniors to see inject so they can see what's going on, and then automatically is criticising the use is super explicitly adding all the dependencies(which it will hide for junior the behaviour). And worst is magic, because inject is working there as a decorator, in order to be able to inject those things. There's a reason of explicity telling things to another dev. I believe that inject on constructor is cleaner because you can use the same idea with other injectors. If you want to reuse those classes in whatever place outside angular, it will still work. Also, you will know explicitly what classes depends and you can use it event without a fancy injector.

Ответить
Joshua Stringfellow
Joshua Stringfellow - 23.05.2023 17:14

Funny/interesting that Spring Framework has shifted from field injection to constructor injection and Angular might be doing the opposite.

Ответить