I am trying to run a sample web application to find out how lazy loading works with Angular modules. I created the application through angular-cli 1.0.0-beta.24, which uses Angular 2.4.1. I created the main application, and then 3 components. Then I added application-level routing and directly referenced the first two components by importing the components into the application module. For the third component, I simply added routing using the loadChildren method specified in Angular routing protocols . My routing configuration for the main application is as follows:
const appRoutes: Routes = [
{ path: 'comp1', component: Comp1Component},
{ path: 'comp2', component: Comp2Component},
{ path: 'comp3', loadChildren: 'app/comp3/comp3.module'}
];
export default RouterModule.forRoot(appRoutes);
I turned the code for the third component into a module and removed all imports from the application into the third component. The routing configuration for the module is as follows:
const routes: Routes = [
{path: '', component:Comp3Component}
];
export default RouterModule.forChild(routes);
When I launch the application, the routes for Comp1 and Comp2 work fine, when I click the link for Comp3 (module route), I get the following error registered on the console:
EXCEPTION: Uncaught (in promise): Error: Cannot find module ./comp3/comp3.module'.
Error: Cannot find module './comp3/comp3.module'.
at webpackEmptyContext (http://localhost:4200/main.bundle.js:77:8) [angular]
Webpack doesn't seem to handle lazy loading. I tried all variations of the "loadChildren" call paths, including:
loadChildren: 'app/comp3/comp3.module#Comp3Module'
no change in behavior (still get the error above). Something new in angular2 might be something in my code, but I can see that others are getting the same error. My google / stackoverflow foo didn't lead me to a solution. I have added my sample application to my Github account here .
, Angular, StackOverflow, plunkr. , - , - , , plunkr ng .
. html : ( github):
<h1>
{{title}}
</h1>
<ul>
<li><a class="nav-link" routerLink="/comp1" routerLinkActive="active">Component 1</a></li>
<li><a class="nav-link" routerLink="/comp2" routerLinkActive="active">Component 2</a></li>
<li><a class="nav-link" routerLink="/comp3" routerLinkActive="active">Component 3</a></li>
</ul>
<p></p>
<router-outlet></router-outlet>
( src/app down) angular2 , tweeks, , angular -cli. , TS, - :
{ path: 'comp3', loadChildren: './comp3/comp3.module#Comp3Module'}
loadChildren. Github, , angular -cli.