简体   繁体   中英

Value Provider error while injecting in Angular

I'm trying to create a Value Provider for our Core Framework.

So i've created thoses

export interface TexteVide {
  texte: string;
}

export const TEXTE_VIDE_VALEUR = new InjectionToken<TexteVide>('TEXTE_VIDE_VALEUR');

export const TexteVideValeurParDefaut: TexteVide = {
  texte: '(vide)'
};

Then i've set the providers of our Shared Module

{ provide: TEXTE_VIDE_VALEUR, useValue: TexteVideValeurParDefaut }

And finally i've injected our Shared Module in other Modules of our application to be able to inject TEXTE_VIDE_VALEUR where needed.

but i'm getting Error: Can't resolve all parameters for TestsComponent: (?).

Here is the constructor of the TestsComponent:

constructor(private TEXTE_VIDE_VALEUR: TexteVide) { console.log(TEXTE_VIDE_VALEUR.texte); }

What's wrong in my implementation ? or am I missing something ?

You should use @Inject() to inject an InjectionToken :

constructor(@Inject(TEXTE_VIDE_VALEUR) private texte: TexteVide) {
  console.log(texte.texte);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM