Thursday, August 13, 2020

Angular Provider Scope

In the previous post, we saw on how to make a service available globally by declaring the @Injectable providedIn: 'root'. We can also declare module-level services using ProvidedIn, in this post we shall see on how to declare module-level service using @Injectable

To limit the scope of a service to a module we need to specify the module in the providedIn instead of ‘root’. In the below code the HellpService is made available only to the HelloModule, the service is not available to any other Modules in the Application.

import { Injectable } from '@angular/core';
import { HelloModule } from './user.module';
@Injectable({
  providedIn: HelloModule,
})
export class HelloService {
}
This is the preferred way to declare module level service, because it enables tree-shaking and optimize the build. This service will be excluded from the bundles created for other modules which don’t have access to this service. 

Search Flipkart Products:
Flipkart.com

No comments: