0

When i import a module in appmodule and call forRoot everything is working no Injection problems. But when i remove from appmodule and add in a lazyloaded module calling forRoot not working as expected. Getting NullInjectorError: No provider for InjectionToken forRoot() , is it mandatory that all modules needs to be imported in appmodule. My main moto is to reduce the main.js bundle size , so don't want to load all modules in appmodule

Appmodule => featureModule.forRoot(env) => working Fine

LazyloadedModule => fetaureModule.forRoot(env) => Null Injector error

2 Answers2

0

Include your routes with RouterModule’s forChild method instead of forRoot

Reference : https://angular.io/guide/lazy-loading-ngmodules

  • Issue is not related to router, lazyloading is working fine am able to go the lazyloaded module , but in lazyloaded module adding forRoot to pass environment variable to another feature module is not working. But works the same in appmodule adding feature module directly – praveen Jul 05 '20 at 16:17
0

Use fetaureModule.forChild(env) instead of fetaureModule.forRoot(env).

For all the lazy-loaded modules, we are supposed to use forChild().

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77