Singleton Services

Go back to Tutorial

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

  • Declare that the service should be provided in the application root.
  • Include the service in the AppModule or in a module that is only imported by the AppModule.

Beginning with Angular 7.0, the preferred way to create a singleton services is to specify on the service that it should be provided in the application root. This is done by setting providedIn to root on the service’s @Injectable decorator:

src/app/user.service.0.ts

import { Injectable } from ‘@angular/core’;

@Injectable({

providedIn: ‘root’,

})

export class UserService {

}

 

Go back to Tutorial

Get industry recognized certification – Contact us

Menu