I am getting a weird error. I am developing angular 4 based project. Following in my code structure
When I try to load first from dashboard, I get following error.
ERROR Error: Uncaught (in promise): Error: Cannot find module 'app/dashboard/first/first.module'.
Error: Cannot find module 'app/dashboard/first/first.module'.
at webpackAsyncContext (src async:14)
at SystemJsNgModuleLoader.webpackJsonp.../../../core/@angular/core.es5.js.SystemJsNgModuleLoader.loadAndCompile (core.es5.js:5644)
at SystemJsNgModuleLoader.webpackJsonp.../../../core/@angular/core.es5.js.SystemJsNgModuleLoader.load (core.es5.js:5632)
at RouterConfigLoader.webpackJsonp.../../../router/@angular/router.es5.js.RouterConfigLoader.loadModuleFactory (router.es5.js:3417)
at RouterConfigLoader.webpackJsonp.../../../router/@angular/router.es5.js.RouterConfigLoader.load (router.es5.js:3401)
at MergeMapSubscriber.project (router.es5.js:1569)
Following is my app.routing.module code
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', loadChildren: 'app/login/login.module' },
{ path: 'dashboard', loadChildren: 'app/dashboard/dashboard.module' },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
At compile time there is no error. However when I add a space in dashboard.routing.module.ts and save the file, the error goes away and the routing works perfectly. It runs smoothly till I restart node server.
Try to change the code like this.
const routes: Routes = [
{ path: '', loadChildren: 'app/login/login.module#loginModule'},
{ path: 'dashboard', loadChildren: 'app/dashboard/dashboard.module#dashboardModule' },
{ path: 'dashboard/first',loadChildren:'app/dashboard/first.module#firstModule'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
see more on loadChildren info Loadchildren concept