Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: Inject Service to another service. (No provider error)

Tags:

angular

I want to inject a service to another service:

@Injectable()  export class Dispatcher { }   @Injectable()  export class TodoStore {      constructor(@Inject(Dispatcher) dispatcher:Dispatcher){       } } 

But I always get Error: No provider for Dispatcher!

Thanks.

like image 228
Ian Zhang Avatar asked May 11 '16 03:05

Ian Zhang


1 Answers

You need to provide your service somewhere. Please refer to angular2 docs

You could provide it in the bootstrap method:

bootstrap(AppComponent,[TodoStore,Dispatcher]); 

or the app component:

@Component({     ...       providers:[TodoStore,Dispatcher] } ... 

Or in any other component, depending on your needs.

Also, you don't need to @Inject(Dispatcher) in the constructor. It's basically the same as

constructor(dispacher:Dispatcher){ } 

Oh yeah, welcome to SO :)

like image 181
Abdulrahman Alsoghayer Avatar answered Sep 17 '22 09:09

Abdulrahman Alsoghayer