Service duplicated with lazy loaded module
See original GitHub issueπ bug report
Affected Package
I guess, the issue is caused by package :
@angular/common @angular/router
Is this a regression?
Nope
Description
I have an injected service in two differents modules. One is lazy loaded, the other is not.
When I dump the value of my injected service, it has two different value.
π¬ Minimal Reproduction
https://github.com/rsaenen/lazy-service
- Start and go to the app
- Open the browser console
- Click on SIGN.IN
- Click on the mini-fab next to βClick hereβ
- Click on the item menu βAND CLICK HEREβ
- Look at the browser console where AuthService is dumped by the two modules.
Here my console:
AuthService {jwtService: JwtService, router: Router, _login$: ReplaySubject, _jwtPayload: {β¦}}
> jwtService: JwtService {_jwt: JwtHelper}
> router: Router {rootComponentType: Ζ, urlSerializer: DefaultUrlSerializer, rootContexts: ChildrenOutletContexts, location: Location, config: Array(3), β¦}
> _jwtPayload: {email: "admin@lazy-service.com", firstname: "firstname", lastname: "lastname", rights: Array(1), exp: 1564753764, β¦}
> _login$: ReplaySubject {_isScalar: false, observers: Array(2), closed: false, isStopped: false, hasError: false, β¦}
> jwtPayload: (...)
> login$: (...)
> token: (...)
AuthService {jwtService: JwtService, router: Router, _login$: ReplaySubject}
> jwtService: JwtService {_jwt: JwtHelper}
> router: Router {rootComponentType: Ζ, urlSerializer: DefaultUrlSerializer, rootContexts: ChildrenOutletContexts, location: Location, config: Array(3), β¦}
> _login$: ReplaySubject {_isScalar: false, observers: Array(0), closed: false, isStopped: false, hasError: false, β¦}
> jwtPayload: (...)
> login$: (...)
> token: (...)
JwtPayload
is undefined when it used by the lazy loaded module.
Note: If the AuthService is provided in root, it works.
π Your Environment
Angular Version:
Angular CLI: 8.1.0
Node: 10.16.0
OS: linux x64
Angular: 8.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.801.0
@angular-devkit/build-angular 0.801.0
@angular-devkit/build-optimizer 0.801.0
@angular-devkit/build-webpack 0.801.0
@angular-devkit/core 8.1.0
@angular-devkit/schematics 8.1.0
@angular/cdk 8.0.2
@angular/language-service 8.1.1
@angular/material 8.0.2
@angular/material-moment-adapter 8.1.1
@ngtools/webpack 8.1.0
@schematics/angular 8.1.0
@schematics/update 0.801.0
rxjs 6.4.0
typescript 3.4.5
webpack
Anything else relevant?
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Angular lazy loading duplicate modules - Stack Overflow
I have 4 modules. Each module is lazy loaded. FirstPageWithTitleModule; SecondPageWithTileModule; ThirdPageWithTitleModule; RandomModule. TheΒ ...
Read more >How to avoid Angular injectable instances duplication - Medium
This is happening because Angular creates a new module Injector for any lazy loaded module, this behavior is perfectly described in docs and...
Read more >Customize Angular lazy loading modules for multiple frontends
Learn how to maximize your code sharing and cleanly separate your different UI components and services for multiple frontend Angular apps.
Read more >Singleton services - Angular
If a module defines both providers and declarations (components, directives, pipes), then loading the module in multiple feature modules would duplicate theΒ ...
Read more >Remove duplicate modules in JavaScript bundles - GTmetrix
JavaScript bundles on a majority of webpages are typically built by importing code from popular libraries, dependencies, and packages. This can often result...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks, I checked the documentation about lazy loading but I didnβt think that what I was looking for was in the singleton services.
I wonder about a good architecture with lots off lazy loaded modules, it appears that lazy load a module is a good pratice but gives you a lot of inconvenients like this one (and SSR for exemple).
I will look for an article βhow to manage big angular appβ.
I think @thefliik is correct since the reasoning behind it is due to the
ModuleInjector
tree. I have a small demo I made on this point a month ago (so it doesnβt exactly do what your demo does, but is related) that might help you: https://github.com/kapunahelewong/module-injector-treeThe root
ModuleInjector
will always serve as a fallback injector, even in the case of lazy loading. In my example, the lazy loaded module can get to the service that isprovidedIn
root
, but theAppModule
canβt see the lazy moduleβs service. Youβll have to clone/download and serve it to see it clearly.I have a PR in review right now that goes in-depth on the injector trees which might help, too: https://github.com/angular/angular/pull/28700. Hereβs a preview of the Hierarchical Injectors page rewrite that might help if you prefer the rendered docs look: https://pr28700-bf33843.ngbuilds.io/guide/hierarchical-dependency-injection
The relevant part is the
ModuleInjector
stuff. If you have any questions on the explanation in there, please let me know.