how do I get angular2 dependency injection to work with value providers

后端 未结 1 1130
遥遥无期
遥遥无期 2021-01-14 08:28

I\'m following the angular docs for Dependency Injection and tried to duplication the section on dependency injection tokens. But it\'s clear I still don\'t get it.

<
相关标签:
1条回答
  • 2021-01-14 09:13

    If you don't use a type as key for the provider but instead a string or OpaqueToken you need to pass the key to @Inject()

    constructor(
        @Inject('CFG_STRING') /* @Optional()*/ cfgString: CFG_STRING
      ) {
    

    and provide it like

      providers: [
        {provide: 'CFG_STRING', useValue: CFG_STRING}
      ],
    

    CFG_STRING is not a type and can therefore not be used as key. Either you use some string or mentioned an OpaqueToken. It can be any string, it just needs to match between provide and @Inject()

    0 讨论(0)
提交回复
热议问题