I'm getting an error when building the project.
ng build --prod
The following Is the error showing on the console.
Date: 2019-05-06T07:11:31.860Z
Hash: 86bc6833bfe9e981c890
Time: 12080ms
chunk {0} runtime.26209474bfa8dc87a77c.js (runtime) 1.41 kB [entry] [rendered]
chunk {1} es2015-polyfills.c5dd28b362270c767b34.js (es2015-polyfills) 56.4 kB [initial] [rendered] chunk {2} main.fa4681da67b0e16d34dc.js (main) 128 bytes [initial] [rendered]
chunk {3} polyfills.41559ea504b9f00b6dea.js (polyfills) 130 bytes [initial] [rendered]
chunk {4} styles.3ff695c00d717f2d2a11.css (styles) 0 bytes [initial] [rendered]
**ERROR in Cannot read property 'loadChildren' of undefined**
Setup
- Windows 10
- node v10.15.3
- npm 6.4.1
- @angular/cli 7.3.8
Repro steps
1) create new angular app with routing
1.1) ng new MyApp
1.2.) when prompted to use routing, say yes and accept the other defaults
2) ng g component Home
3) ng g component Settings
4) And modify the app-routing.module.ts with following.
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { SettingsComponent } from './settings/settings.component';
const routes: Routes = [
{
path: "home",
component: HomeComponent
},
{
path: "settings",
component: SettingsComponent
}
];
const routes2 = routes.map( e => ({ path: e.path, component: e.component}) );
const routes3 = [];
routes.forEach( e => ( routes3.push({ path: e.path, component: e.component})) );
@NgModule({
imports: [
// RouterModule.forRoot(routes2), // **this doesn't work**
RouterModule.forRoot(routes3) // but this one works just fine
],
exports: [RouterModule]
})
export class AppRoutingModule { }
5) ng build --prod
Error should appear in the console
The question
Looking at step 4, my question really is why is that the following works?
RouterModule.forRoot(routes3)
whereas the following doesn't work?
RouterModule.forRoot(routes2)
Note that routes
, routes2
, and routes3
are all the same, at least equivalent, looking at their structure. They are all just arrays of objects with the same attributes. Not sure what I'm missing here.
UPDATE:
The following surprisingly doesn't work too.
RouterModule.forRoot( routes3.concat([]) );
But the following works.
RouterModule.forRoot( routes3 );
This is really strange. It must be a bug in the compiler.