When you are working in Angular 2, you might got stuck in a situation where Angular 2 is creating multiple instances of your service. In Angular 2, Service are singleton object which can be used to fetch and push data from multiple components.
First of all, try to understand that you only need to add your service once either in @NgModule or AppComponent(Top level component).
//Services added in NgModule inside providers array
//In AppComponent using providers array
If you define your service in both of NgModule & AppComponent provider array than in that case 2 instances of that service will get created.
I am writing below the fewer reasons which could lead to Multiple instances of services.
- Specifying the wrong file names. Make sure you have type file name correctly.
- Adding your service in Provider array of multiple component having parent child relationship.
- Also have seen people to type Injector instead of Provider, don’t do that mistake. Syntax should be correct.
That’s it. Build app and happiness …. 🙂