Sharing the Same Dependency Injection Tree

So far our problem is that we are creating two instances of the same services in different levels of the DI tree. The instance created in the lower branch of the tree is shadowing the one defined at the root level. The solution? To avoid creating a second instance in a lower level of the DI tree for the lazy loaded module and only use the service instance registered at the root of the tree.

To accomplish that, we need to modify the definition of the SharedModule and instead of defining our service in the providers property, we need to create a static method called forRoot that exports the service along with the module itself.

app/shared/shared.module.ts

  1. import { NgModule, ModuleWithProviders } from '@angular/core';
  2. import { CounterService } from './counter.service';
  3. @NgModule({})
  4. export class SharedModule {
  5. static forRoot(): ModuleWithProviders {
  6. return {
  7. ngModule: SharedModule,
  8. providers: [CounterService]
  9. };
  10. }
  11. }

With this setup, we can import this module in our root module AppModule calling the forRoot method to register the module and the service.

app/app.module.ts

  1. ...
  2. import { SharedModule } from './shared/shared.module';
  3. @NgModule({
  4. imports: [
  5. SharedModule.forRoot(),
  6. ...
  7. ],
  8. ...
  9. })
  10. export class AppModule {}

We should only call forRoot in the root application module and no where else. This ensures that only a single instance of your service exists at the root level. Calling forRoot in another module can register the service again in a different level of the DI tree.

Since SharedModule only consists of a service that Angular registers in the root app injector, we do not need to import it in LazyModule. This is because the lazy loaded module will already have access to services defined at the root level.

View Example

This time, whenever we change the value of the counter property, this value is shared between the EagerComponent and the LazyComponent proving that we are using the same instance of the CounterService.

However it is very likely that we may have a component, pipe or directive defined in SharedModule that we'll need in another module. Take the following for example.

app/shared/shared.module.ts

  1. import { NgModule, ModuleWithProviders } from '@angular/core';
  2. import { CounterService } from './counter.service';
  3. import { HighlightDirective } from './highlight.directive';
  4. @NgModule({
  5. declarations: [HighlightDirective],
  6. exports: [ HighlightDirective ]
  7. })
  8. export class SharedModule {
  9. static forRoot(): ModuleWithProviders {
  10. return {
  11. ngModule: SharedModule,
  12. providers: [CounterService]
  13. };
  14. }
  15. }

In here, we declare and export HighlightDirective so other modules that import SharedModule can use it in their templates. This means we can just import the module in LazyModule normally.

app/lazy/lazy.module.ts

  1. import { NgModule } from '@angular/core';
  2. import { SharedModule } from '../shared/shared.module';
  3. import { LazyComponent } from './lazy.component';
  4. import { routing } from './lazy.routing';
  5. @NgModule({
  6. imports: [
  7. SharedModule,
  8. routing
  9. ],
  10. declarations: [LazyComponent]
  11. })
  12. export class LazyModule {}

Now we can use this directive within LazyModule without creating another instance of CounterService.

View Example

原文: https://angular-2-training-book.rangle.io/handout/modules/shared-di-tree.html