SharedModuleand instead of defining our service in the
providersproperty, we need to create a static method called
forRootthat exports the service along with the module itself.
forRootmethod to register the module and the service.
forRootin the root application module and no where else. This ensures that only a single instance of your service exists at the root level. Calling
forRootin another module can register the service again in a different level of the DI tree.
SharedModuleonly 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.
counterproperty, this value is shared between the
LazyComponentproving that we are using the same instance of the
SharedModulethat we'll need in another module. Take the following for example.
HighlightDirectiveso other modules that import
SharedModulecan use it in their templates. This means we can just import the module in
LazyModulewithout creating another instance of