#Injection token not getting provided in lazy loading module
12 messages · Page 1 of 1 (latest)
the token it will be use by an APIModule(LazyLoadingApiModule )
OK, but to do what? And where is your complete minimal repro on stackblitz? Here's one, but it doesn't reproduce your issue: https://stackblitz.com/edit/angular-ivy-ts-strict-enabled-bj4k1j?file=src%2Fapp%2Fapp.module.ts,src%2Fapp%2Flazy.module.ts,src%2Fapp%2Fapp.component.html
The APIModule will be responsible to call the API. Here it's the stackblitz: https://stackblitz.com/edit/angular-5bnobf?file=src/app/lazy-loading/lazy-loading.module.ts
You're adding a module in providers, which isn't correct. And you're not injecting your BASE_PATH anywhere, so how can you claim that the value is not injected? And again, What is the role of this token in the first place. Why, in your mind, is it useful or necessary to define such a provider?
Oh, and you have not actually defined the BASE_PATH injection token anywhere. You've used declare, which means to typescript: "trust me, such a variable is defined somewhere by another script".
Thank you for your answers, you are right. I updated the stackblitz, I hope now it's more clear
You're still providing a module, which is incorrect.
And your service is providedIn: 'root', so it's provided by the root injector, and not by the injector of the lazy-loaded module. So it can't possibly have access to the token (which BTW, is still not defined anywhere).
I think you're making your own life much more complex than it should be:
- why having so many modules (or even, why having any module at all)?
- why having such a token instead of just sending requests to
/templates-service? - why would this token be defined in a lazy-loaded module?
- why would you have an ApiModule?
in this way is the project structure, if I will provide the BASE_PATH and the APIModule in the MainModule the value is injected
Yeah, my point is precisely that this project structure is way too complex, for no benefit that I can see. And again: providing a module makes no sense at all. Modules are not supposed to be provided.
Angular is promoting an architecture without NgModule. And you're creating plenty of useless modules, with useless providers.
ok, but the application is pretty complex and by using modules we group different sections on the app and each sections contains multiple components
From what you've posted, it's complex mainly because you add unnecessary complexity. Anyway, keep your modules and your tokens if you want, but understand that
- you can't use an InjectionToken that doesn't exist
- a service provided by the root injector can't use a token provided by a lazy-loaded module
- adding an NgModule to the providers of another NgModule doesn't make sense