You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
How to Lazy Load Components in Angular 4 in Three Steps
Episode 23 written by Jeff DelaneyHealth Check: This lesson was last reviewed on and tested with these packages:
- Angular v6.0.4
Update Notes: Updated code snippets for Angular v6
Find an issue? Let's fix it
Lazy loading is a technique in Angular that allows you to load JavaScript components asynchronously when a specific route is activated. This can add some initial performance during the initial load, especially if you have many components with complex routing. There are some good posts about lazy loading in angular, but I wanted to simplify it further. This lesson will show you how to enable lazy loading in 3 easy steps with a brand new app.
Step 1 - Create a new App with Routing
Our app will load the AppComponent
by default at the root URL, then when the user navigates to lazy/load-me
, the lazy module will be loaded asynchronously.
ng new lazyDemo --routing |
Now add a link to the lazy url in the app component.
<button routerLink="/lazy/load-me"></button> |
Step 2 - Generate the “Lazy” Module
Let’s create a module that will be lazy loaded, along with a couple components. The --flat
flag prevents a directory from being created, then we can easily add components to the module via the Angular CLI.
ng g module lazy --flat |
Inside the lazy module we need to import the RouterModule
. Two things to notice here:
- The route path to the component is
path: 'load-me'
, even though it will actually be activated onlazy/load-me
. This is a child route, which you will see come together in step 3 - The
RouterModule
callsforChild(routes)
.
import { NgModule } from '@angular/core'; |
And here’s what the LazyParentComponent
looks like. Just looping over the child component a few times.
<div *ngFor="let name of ['Foo', 'Bar', 'Baz']"> |
Your Angular CLI config might append an app
prefix to the component selector name. If so, make sure remove the prefix and for the last section to work properly. For example…@Component({ selector: 'lazy-child' })
Step 3 - Point the App Router to the Lazy Module
The final step is to point the lazy route to the lazy module from the app router. We can do this with the loadChildren
property with the path to the module file, then reference the module itself with a hash #. This tells angular to only load LazyModule
when the lazy url is activated.
import { NgModule } from '@angular/core'; |
Verify Lazy Loading is Working
Let’s make sure Lazy Loading is working. In chrome, open developer tools and click the network tab. When you navigate to the lazy url, you should see a 0.chunk.js
file rendered. In this demo, you can see it took 2ms to load.
The lazy loaded module is the last chunk.js file in the chrome network logs
When you take a look inside this file, you should see a bunch of webpack JavaScript code with related to the lazy module and its components.
Looking inside, we see that is a bunch of JavaScript webpack code
That’s it for lazy loading components in Angular. Good luck!