Learn How to Use the Inject Method in Angular 14

Поділитися
Вставка
  • Опубліковано 16 вер 2024

КОМЕНТАРІ • 37

  • @ronavraham-r3h
    @ronavraham-r3h Рік тому +1

    WOW!
    great example for inject. first time I subscribe after one video watch

  • @owenjaspervargas1408
    @owenjaspervargas1408 Рік тому +1

    Good teacher! Easy to understand you deserve more subs!

  • @arthurfedotiew3609
    @arthurfedotiew3609 2 роки тому +2

    Thank you master Profanis!)
    The most valuable one thing to learn from this is that inheritance begins to seem a little more useful now)
    Btw, inject() in a context of factory provider could be used for a pretty long time, I think when ivy got into a play or so

    • @CodeShotsWithProfanis
      @CodeShotsWithProfanis  2 роки тому

      Yeah, the inject() opens so many possibilities.
      Ohh, thanks for letting me know about the factory! :)

  • @paulh6933
    @paulh6933 2 роки тому +2

    awesome info

  • @nfallycoly6362
    @nfallycoly6362 Рік тому +1

    Awesome, thank you so much!

  • @harunmn
    @harunmn 2 роки тому +1

    awesome explanation! thanks

  • @hadirahmani7943
    @hadirahmani7943 2 роки тому +1

    that was very powerful, thanks so much

  • @orshalmayev7077
    @orshalmayev7077 2 роки тому +1

    This is usefull.
    Thank you!

  • @ThodorisHadoulis
    @ThodorisHadoulis 2 роки тому +1

    Really insightful!

  • @diyvideos9737
    @diyvideos9737 2 роки тому +1

    Thanks

  • @antt0023
    @antt0023 2 роки тому +3

    How did you make the param name to show on vscode? Like in the logger token, it shows _desc as param name. Really helpful video. Thanks man

    • @CodeShotsWithProfanis
      @CodeShotsWithProfanis  2 роки тому +5

      I really like the feature with the argument names :)
      Add this in your settings.json:
      "typescript.inlayHints.parameterNames.enabled": "all"
      Glad you liked the video!

  • @XHANITRUNGUMANOS
    @XHANITRUNGUMANOS 2 роки тому +1

    Hello . I would like to add that we can use the Inject method in any other hook or lifecycle , except from constructor utilizing the power of closures, as we are able to store the injected token inside the closure scope and still use values in the returned function. 😁😁😁|
    exort const dispatchSearch = (): ((search: string) => void) => {
    const store = inject(Store);
    return (search) => store.dispatch(new SetSearch(search))
    }

  • @rafaelpanayiotou1833
    @rafaelpanayiotou1833 2 роки тому +1

    Hello.. thanks for your video...! why i am facing this issue? " NG0203: inject() must be called from an injection context"

    • @CodeShotsWithProfanis
      @CodeShotsWithProfanis  2 роки тому

      The inject() method works only during the class construction. Having said that, it's OK to use the inject in the constructor, but it's too late if you try to use this in the ngOnInit hook.
      The inject requires the initialization context, which is available only during the class construction.

  • @fahadgaliwango4502
    @fahadgaliwango4502 2 роки тому +1

    Thak you profanis .
    Have understood it as way to reduce dependecies in the constructor , so they can be declared as global functions or variables, is that a correct take home

    • @CodeShotsWithProfanis
      @CodeShotsWithProfanis  2 роки тому +1

      Yes it's a way to have utility functions, create more abstractions. It offers a great flexibility.

  • @OlehBiblyi
    @OlehBiblyi 2 роки тому +1

    Thank you, very interesting, I think I need to repeat a lot of stuff about Class, to properly understand what is going on here ). Also what is a difference between take(1) and first()?

    • @CodeShotsWithProfanis
      @CodeShotsWithProfanis  2 роки тому +1

      The difference between take(1) and first() operators is in the emitted value.
      If you use the first() operator and the stream completes without any emitted value, then an error is thrown.
      However, the take(1) will complete the stream without error.
      I hope this helps

    • @OlehBiblyi
      @OlehBiblyi 2 роки тому

      @@CodeShotsWithProfanis Thanks a lot!

  • @seanpheneger6632
    @seanpheneger6632 2 роки тому +1

    Pluck is being deprecated. You should update to use map operator

    • @CodeShotsWithProfanis
      @CodeShotsWithProfanis  2 роки тому +1

      Ohh yeah, indeed! rxjs.dev/api/index/function/pluck
      Thanks for pointing out this

    • @seanpheneger6632
      @seanpheneger6632 2 роки тому +1

      Yeah, sure thing. I used it a lot in a few of my older projects but have been updating any new files to use map now so it'll be less painful of a migration when it is fully removed. Thanks for the video. It was insightful.

  • @zshn
    @zshn Рік тому

    Well, that's a mess. inject() anywhere and everywhere. Peer Reviews, debugging and defect fixing are going to be more fun.

  • @jonasjanaitis436
    @jonasjanaitis436 2 роки тому +1

    I spen half a day figuring out what a hell is going on. In one code I found this implementation: constructor(@Inject(HttpClient) private http: HttpClient). But this constructor for me always returned undefined...I think it worked in older versions of Angular. At least now I can go to sleep in relief, thank you

    • @CodeShotsWithProfanis
      @CodeShotsWithProfanis  2 роки тому

      Both inject and @Inject are valid per case. In case you want to use the HttpClient in your constructor, you can even do
      constructor(private http: HttpClient) {}

    • @jonasjanaitis436
      @jonasjanaitis436 2 роки тому +1

      @@CodeShotsWithProfanis I have Rest class that is not marked as @Injectable but it is like an abstract class. Then I have two services that extend that Rest class and is marked as @Injectable and these services are in provider[]. Rest class has HttpClient, but as I said before it always returns undefined when I do constructor(@Inject(HttpClient) private http: HttpClient) in Rest class. But http:HttpClient = inject(HttpClient) works fine.