You can achieve this with the lazy loading feature of RouterModule.
Configure your app.module.ts like the following. The loadChildren
-property must point to the destination of the module and after the hash must be the name of the module.
const routes: Routes = [
{
path: 'landing-page',
loadChildren: './landing-page/landing-page.module#LandingPageModule'
},
{
path: 'another-page',
loadChildren: './another-page/another-page.module#AnotherPageModule'
}
];
@NgModule({
declarations: [
AppComponent
],
imports: [
RouterModule.forRoot(routes),
...
],
providers: [ ... ],
bootstrap: [AppComponent]
})
export class AppModule { }
Place the router-outlet inside of your html if not already done:
<router-outlet></router-outlet>
Then configure your page-modules like this:
const routes: Routes = [
{ path: '', component: LandingPageComponent },
...
];
@NgModule({
imports: [
RouterModule.forChild(routes),
...
],
declarations: [ ... ],
providers: [ ... ]
})
export class LandingPageModule { }
This produces a chunk for each module. In my project, it looks like this:

And when I open my site, i only the requred chunks for the current page will be loaded:
