forRoot in Angular libraries not workig , but in appmodule its working as expected

1.1k views Asked by At

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

There are 2 answers

1
Yogeshkumar On

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

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

0
Ranjithprabhu K On

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

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