Providing a singleton service on Angular

There are two ways to make a service a singleton in Angular:

  • Declare root for the value of the @Injectable() providedIn property
  • Include the service in the AppModule or in a module that is only imported by the AppModule

Using providedIn

import { Injectable } from '@angular/core';

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

NgModule providers array

@NgModule({
  ...
  providers: [UserService],
  ...
})

References
https://angular.io/guide/singleton-services