Angular 4: When and why is @Inject is used in constructor?

后端 未结 3 818
孤街浪徒
孤街浪徒 2021-02-01 16:53

Problem Statment

I am learning Angular 4 and I have stumble upon a code where @Inject is being used in a constructor and I am

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 17:35

    If MAT_DIALOG_DATA is a non-factory/class dependency (like string for your configs), you usually use @Inject.

    Also check InjectionToken: https://angular.io/guide/dependency-injection#injectiontoken

    One solution to choosing a provider token for non-class dependencies is to define and use an InjectionToken


    Here's a plunker: http://plnkr.co/edit/GAsVdGfeRpASiBEy66Pu?p=preview

    if you remove @Inject in these cases you will receive a

    Can't resolve all parameters for ComponentName: (?)

提交回复
热议问题