DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

Angular 17 - Lazy-loading with @defer block

You can lazy-load any standalone component anywhere, anytime, and on your terms, as you can decide the trigger to load that component.

Image description

The above code will load the HelloComponent as soon as the browser is idle, which means it's done loading everything else. While that is happening, we can decide to display a placeholder, and in my case, I decided that such a placeholder would be displayed for a minimum of 2 seconds no matter what. You can use seconds (s) or milliseconds (ms) as a time unit:

Image description

We can also specify a loading template and an error template, all customizable with a minimum amount of time during which we would display those templates:

Image description

Note that @loading supports an "after " option only to show the loading template if loading takes more than a certain amount of time, so you don't have to display anything if the component loads quickly. Both parameters are optional:

Image description

These are the different new blocks available with @defer


Let's get connected! You can find me on:

Top comments (0)