Web / Angular Interview questions
How do I create a service as a singleton in Angular?
We can create a service as singleton in Angular in 2 ways:
Declare that the service should be provided in the application root. This is done by setting
providedInto root on the service's @Injectable decorator as shown below. This is the preferred way to create a singleton service starting Angular 6.import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root', }) export class UserService { }
- Include the service in the AppModule or in a module that is only imported by the AppModule.
More Related questions...