角度2:将服务注入到另一个服务. (没有提供程序错误) [英] Angular 2: Inject Service to another service. (No provider error)
本文介绍了角度2:将服务注入到另一个服务. (没有提供程序错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一项服务注入另一项服务:
I want to inject a service to another service:
@Injectable()
export class Dispatcher {
}
@Injectable()
export class TodoStore {
constructor(@Inject(Dispatcher) dispatcher:Dispatcher){
}
}
但我总是收到错误消息:Dispatcher没有提供者!
But I always get Error: No provider for Dispatcher!
谢谢.
推荐答案
您需要在某处provide
服务.请参考 angular2文档
You need to provide
your service somewhere. Please refer to angular2 docs
您可以在bootstrap方法中提供它:
You could provide it in the bootstrap method:
bootstrap(AppComponent,[TodoStore,Dispatcher]);
或应用程序组件:
@Component({
...
providers:[TodoStore,Dispatcher]
}
...
或在任何其他组件中,具体取决于您的需求.
Or in any other component, depending on your needs.
此外,您无需在构造函数中使用@Inject(Dispatcher)
.基本上和
Also, you don't need to @Inject(Dispatcher)
in the constructor. It's basically the same as
constructor(dispacher:Dispatcher){
}
哦,是的,欢迎来到SO:)
Oh yeah, welcome to SO :)
这篇关于角度2:将服务注入到另一个服务. (没有提供程序错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文