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
providedIn: 'root'
is the easiest and most efficient way to provide services since Angular 6:
For further informations consider reading the documentation and NgModule FAQs
Btw:
*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