Error: Multiple instances of services

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).

If you define your service in both of NgModule & AppComponent provider array than in that case 2 instances of that service will get created.

Also Read:

execute function inside template – Angular 2

Search pipe in Angular 2

Angular 2 attribute binding

I am writing below the fewer reasons which could lead to Multiple instances of services.

  1. Specifying the wrong file names. Make sure you have type file name correctly.
  2. Adding your service in Provider array of multiple component having parent child relationship.
  3. 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 …. 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *