lazy loading not working in Angular 6
I'm getting this error when try to navigate to 'http://localhost:4200/dashboard' lazy loading route in angualr, angualr-cli 6.0.0
ERROR Error: Uncaught (in promise): Error: Cannot find module "app/dashboard/dashboard.module". Error: Cannot find module "app/dashboard/dashboard.module". at $_lazy_route_resource lazy namespace object:5
const routes: Routes = [
{
path: 'login',
loadChildren: 'app/login/login.module#LoginModule',
},
{
path: '',
component: MasterComponent,
children: [
{
path: 'dashboard',
loadChildren: 'app/dashboard/dashboard.module#DashboardModule'
}
],
},
{
path: '**',
redirectTo: 'login
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: []
})
export class AppRoutingModule {
}
@izakgl: thanks a lot!
I am certain you just saved me a lot of time.
Adding export to my loadConfig factory function (APP_INITIALIZER) solved the error with the lazy loaded modules:
export function loadConfig(config: ConfigService): () => Promise<boolean> { ... }
Path in app-routing.modules looks like:
{
path: 'dashboard',
loadChildren: './dashboard/dashboard.module#DashboardModule',
},
If someone of you is working with Nx monorepo, then please check this answer.
I was facing the same issue. i found there could be two case for such problem.
- Provide the absolute path in routes. i.e {path: 'admin', loadChildren:() => AdminModule }
- We don't not need to inject lazy module in App.module.ts. As we importing "RouterModule.forRoot(ROUTES, { useHash: false , scrollPositionRestoration: 'enabled'})"
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.