Lazy Loading of modules
In Angular 2 / 4, you can divide the various functionalities / features of your application into modules. Taking the example of our Photo Albums application, Home page/view , where we show the albums of the user, could be the default/root module. The 'Photos' view, that shows photos inside an album doesn't need to be loaded from the starting of the app, it could be loaded lazily later when user selects an album. We can wrap all the components needed for Photos view in a child module of the app's root module . Similarly, the 'User Settings' component could be wrapped in a separate child module. And then we can configure our Router to load Photos and User Settings modules lazily.
This helps to bring down the initial size of the application bundle thereby improving performance of the application.
Lets say, we have 2 components used in an album's Photos
view: 1. PhotosGrid - a grid view of photos in an album 2. PhotoDetails - shows an individual photo along with its details like date and caption for each photo.
In order to have these components loaded lazily, we have to include these components inside a separate module, lets call it 'photos' module created under photos
folder:
1. First setup the routing for the photos module:
./app/modules/photos/photos.routes.ts
2. Next, use this router in the photos
module:
photos
module:./app/modules/photos/photos.module.ts
Note the use of RouterModule's
forChild
method above.
3. Use loadChildren
in the root module's route configuration
loadChildren
in the root module's route configurationFinally, use loadChildren
in the root module's route configuration
Last updated