Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
DI: Resolve Guard and Component both create a service which is 'provided' in a lazy loaded module #13870
I'm submitting a ... (check one with "x")
I've encountered an issue where a resolve route-guard did not share the same service with a component that is registered to the same module the Resolve guard is.
I have a scenario where the resolve guard of a route bound to a lazy loaded module retrieves a list of data from the backend and caches it on a variable within the service. The same service has another method (that is called from a component) which retrieves fine grained data, first by looking within the cached data, and if not found retrievs that particular subset of data from backend. The issue is that the the Service is recreated when the component is visited as well, thus losing the cached data.
I've been boggling my mind around this issue and I cannot seem to make sense of what is happening. I've read the docs on dependency injection, hierarchical injection and modules. From my understanding the application has a root injector which is bound to the AppModule that is bootstrapped. Child injectors are created for each component, and for each lazy loaded module. Whenever a Service is needed, first the service is looked within the injector bound to the Component, and if not found, checkes in the parent component, and continues going up through the injector tree until reaching the module injector (and if not found here throws an error).
Now if the Service I needed is 'provided' within the lazy loaded module, shouldn't the Resolve guard and the component share the same Singleton Service bound to the module injector? I'm reporting this as a bug because 1) the docs does imply that, and 2) this problem seems to be occuring only with injectors of lazy loaded modules, and not the root module. See the following plunker.
When a service bound to the root module is used by both the Resolve guard and the Component, the service is shared... However when the same logic is applied to the lazy loaded module, the service is created and recreated both when the Resolve Guard is instantiated and when the Component is instantiated respectively. The plunker above demos it. The logic for the retrieval of data in the root module and lazy loaded module is the same (different services/components ofcourse), however the creation of the service behave differently.
Minimal reproduction of the problem with instructions
Please tell us about your environment:
Operating system: windows 10
I have the same problem. I want to initialize some things in services. I have set a CanActivate guard for the root component of the lazy loaded feature module. The guard should initialize all the services before starting the feature module. This works. But then, when I access a service from a component the service is created again. Maybe someone has work around for this.