#Injection token not getting provided in lazy loading module

12 messages · Page 1 of 1 (latest)

wheat moss
#

Post a complete minimal repro as a stackblitz. But also, what's the point of this token?

solid yacht
#

the token it will be use by an APIModule(LazyLoadingApiModule )

wheat moss
solid yacht
wheat moss
#

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".

solid yacht
#

Thank you for your answers, you are right. I updated the stackblitz, I hope now it's more clear

wheat moss
#

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?
solid yacht
#

in this way is the project structure, if I will provide the BASE_PATH and the APIModule in the MainModule the value is injected

wheat moss
#

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.

solid yacht
#

ok, but the application is pretty complex and by using modules we group different sections on the app and each sections contains multiple components

wheat moss
#

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