简体   繁体   English

在Angular中注入时,Value Provider错误

[英]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. 最后,我将共享模块注入了应用程序的其他模块中,以便能够在需要时注入TEXTE_VIDE_VALEUR。

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

Here is the constructor of the TestsComponent: 这是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 : 您应该使用@Inject()注入InjectionToken

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular 4错误:未捕获(承诺):错误:没有ConnectionBackend的提供程序! 在注入Jsonp时 - angular 4 Error: Uncaught (in promise): Error: No provider for ConnectionBackend! while injecting Jsonp Angular DI:将价值代币注入工厂提供者 - Angular DI: Injecting a value token into factory provider Angular2-将服务注入服务时出现无效的提供程序错误 - Angular2 - Invalid provider error when injecting service into service 如何在Angular 2中添加服务并在注入时出错? - How to add service in Angular 2 getting error while injecting? 注入Angular单元测试时找不到提供程序? - Provider not found when injecting in Angular unit tests? 在服务中注入http会给出“没有Http提供程序!”错误 - Injecting http in a service gives “No provider for Http!” error 注入Http时出现Angular 2错误 - Angular 2 error when injecting Http Angular2:使用自定义装饰器或注释将提供程序注入组件? - Angular2: Injecting a provider into a component using a custom decorator or annotation? Angular:在监控变化的同时注入 NgControl - Angular: Injecting NgControl While Monitoring Changes Angular2 - 将http注入自定义服务时,“无法解析所有参数”错误 - Angular2 - 'Can't resolve all parameters' error while injecting http into a custom service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM