Angular: Lazy loading modules with services

karthikaruna picture karthikaruna · Jan 10, 2018 · Viewed 21.4k times · Source

I've been following this tutorial, to understand lazy loading, and below is my inference.

Scenario 1: Services are provided by putting them in the providers array of a child module

Scenario 2: Services are provided in a child module using the forRoot approach

With scenario 1 in context,

  • If a child module is eagerly loaded, an instance of the service is added to the root injector.
  • If a child module is lazily loaded, an instance of the service is added to the root injector and a new instance of the service is added to the child injector, which is not the usual use case.

With scenario 2 in context,

  • If a child module is eagerly loaded, an instance of the service is added to the root injector.

  • If a child module is lazily loaded, the same instance of the service is available in both the root and the child module, which is the usual use case.

They have mentioned the following.

At the beginning,

So, even when using modules, there's no way to have a "private" service unless... the module is being lazy loaded.

Finally,

Although this syntax is a little more convoluted than the original, it will guarantee us that only one instance of the CreditCardService is added to the root module. When the CreditCardModule is loaded (even lazy loaded), no new instance of that service is going to be added to the child injector.

If the instance is going to be available in the root injector as well, how do they say that the service is 'privitized'?

I'm confused. Someone please clarify.

Answer

Mick picture Mick · Oct 26, 2018

providedIn: 'root' is the easiest and most efficient way to provide services since Angular 6:

  1. The service will be available application wide as a singleton with no need to add it to a module's providers array (like Angular <= 5).
  2. If the service is only used within a lazy loaded module it will be lazy loaded with that module
  3. If it is never used it will not be contained in the build (tree shaked).

For further informations consider reading the documentation and NgModule FAQs

Btw:

  1. If you don't want an application-wide singleton, use the provider's array of a component instead.
  2. If you want to limit the scope so no other developer will ever use your service outside of a particular module, use the provider's array of NgModule instead.*

*UPDATE

'use the provider's array of NgModule instead' means to use the providers array of the lazy loaded module, eg:

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

import { UserService } from './user.service';

@NgModule({
  providers: [UserService],
})
export class UserModule {
}

OR to actually name the module in the injectable decorator:

import { Injectable } from '@angular/core';
import { UserModule } from './user.module';

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

Quote from the docs:

When the router creates a component within the lazy-loaded context, Angular prefers service instances created from these providers to the service instances of the application root injector.

Doc ref: https://angular.io/guide/providers#providedin-and-ngmodules