Singleton service in Angular

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

  • Set the providedIn property of the @Injectable() to "root".
  • Include the service in the AppModule or in a module that is only imported by the AppModule
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class UserService {
}
Published
Categorized as Angular

By Santosh Kumar Shah

I am JavaScript developer.

Leave a comment

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