angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

lazy loading not working in Angular 6

Open cagline opened this issue 8 years ago • 44 comments

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 {
}

cagline avatar May 06 '18 07:05 cagline

@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', },

schreibse avatar Apr 15 '19 07:04 schreibse

If someone of you is working with Nx monorepo, then please check this answer.

danieldanielecki avatar Apr 15 '19 13:04 danieldanielecki

I was facing the same issue. i found there could be two case for such problem.

  1. Provide the absolute path in routes. i.e {path: 'admin', loadChildren:() => AdminModule }
  2. We don't not need to inject lazy module in App.module.ts. As we importing "RouterModule.forRoot(ROUTES, { useHash: false , scrollPositionRestoration: 'enabled'})"

prashuMishra avatar May 29 '19 07:05 prashuMishra

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.