Angular service instantiated twice? Angular service instantiated twice? angular angular

Angular service instantiated twice?


Application wide singletons must be defined on the bootstrapped module:

platformBrowserDynamic()  .bootstrapModule(AppModule)@NgModule({  providers: [SingletonService1, SingletonService2],  bootstrap: [AppComponent]})export class AppModule {}

source

or by setting providedIn: 'root' on the service decorator:

@Injectable({  providedIn: 'root',})export class UserService {}

source


In my case the service was instantiated twice, because I imported the service using two different approaches (my IDE (VS2019) mishelped me here by automatically generating the incorrect import):

import { Service } from '@mycompany/mymodule' 

and

import { Service } from '../../../dist/@mycompany/mymodule/@mycompany-mymodule';


Visual code import my service in this way automatically

import { ConfigService } from 'src/app/services/config.services.js';

And the correct way is:

import { ConfigService } from 'src/app/services/config.services';