Angular 17 has introduced a new feature called Deferrable Views, which allows developers to defer the rendering of certain components until they are actually needed. This can improve the performance of your application by reducing the initial load time and only rendering components when they are actually needed.
In this tutorial, we will explore how to use Deferrable Views in Angular 17 and how it can benefit your application.
- Setting up your Angular project
First, make sure you have Node.js and Angular CLI installed on your machine. You can install them by running the following commands in your terminal:
npm install -g @angular/cli
Next, create a new Angular project by running the following command:
ng new my-deferrable-views-app
Navigate into your project directory by running:
cd my-deferrable-views-app
- Creating a deferrable view
To create a deferrable view in Angular, you need to use the deferred
attribute in the component’s template. This attribute tells Angular to defer the rendering of the component until it is actually needed.
For example, let’s create a simple component with a deferrable view. First, generate a new component by running:
ng generate component lazy-loaded
Next, open the lazy-loaded.component.html
file in the app/lazy-loaded
directory, and add the deferred
attribute to the root element of the template:
<div deferred>
<p>This is a deferrable view!</p>
</div>
- Lazy loading the deferrable view
To actually defer the rendering of the component, we need to lazy load it in our application. We can use Angular’s routing system to achieve this.
Open the app-routing.module.ts
file in the src/app
directory, and modify it to lazy load the LazyLoadedComponent
when a certain route is accessed:
const routes: Routes = [
{ path: 'lazy-loaded', loadChildren: () => import('./lazy-loaded/lazy-loaded.module').then((m) => m.LazyLoadedModule) },
];
Next, create a new module for the LazyLoadedComponent
by running:
ng generate module lazy-loaded/lazy-loaded --route lazy-loaded --module app.module
This will generate a new module and route for the LazyLoadedComponent
, and configure it for lazy loading.
- Testing the deferrable view
Now that we have created a deferrable view and lazy loaded it in our application, let’s test it out.
Start your Angular development server by running:
ng serve
Navigate to http://localhost:4200/lazy-loaded
in your browser, and you should see the deferrable view rendered on the page. If you inspect the DOM, you will notice that the component was only rendered when the route was accessed, demonstrating the power of deferrable views in Angular 17.
Conclusion
Deferrable Views are a powerful new feature in Angular 17 that allows you to defer the rendering of certain components until they are actually needed. This can significantly improve the performance of your application by reducing the initial load time and only rendering components when they are required.
In this tutorial, we covered how to create a deferrable view in Angular, lazy load it in the application, and test it out. I hope you found this tutorial helpful and that you can leverage deferrable views to optimize your Angular application.
Level up your Angular skills with my Advanced Courses 🚀
https://bit.ly/discounted-course-bundle
💡 Short Frontend Snacks (Tips) every week here:
Twitter – https://twitter.com/DecodedFrontend
Instagram – https://www.instagram.com/decodedfrontend
LinkedIn – https://www.linkedin.com/in/dmezhenskyi
If we use Heavycomponent in import section of app component, then is it not contained in bundle size of app component? If yes then how defer is benifit?
Thank you very much, another really great explanation and use cases! 🙂
Beautifully explained! This is going to be very useful.
How the @loading, @placeholder etc… knows which component they are waiting, since I can have multiple instances of the same "section"?
Fantastic explanation
awesome feature thanks sensei 😍
I always get confused about how angular projects, and frontend in general is deployed/retrieved by the use , i mean, there is a server that sends chunks and the user receive those chunks and the browser generate the js to create the spa functionality, but then comes in server side rendering and SSG and it gets a bit confusing to get the whole picture of what is happening, do you have a video talking about that? or could you do one? I like your advanced-oriented way of making angular videos.
what is the difference between "when" and "prefetch when"? Why do I need "prefetch when" if I could load content by just "when"?
no one could teach deferrable views in angular better.
Bravo !! Big thanks <3 bien expliqué, j'apprécie beaucoup votre efforts <3
Thanks for the clear explanation.
I have a doubt. U said placeholders, loading error are eagerly loaded.
But in projects we use again another component inside the error and loading blocks
Then we lose the lazy loading right. So how can we achieve even loading the blocks content as lazy loaded.
super awesome
Fc from thailand
is this aplication SSR? I have a problem with @loading , doesn't show the content inside loading for example
@defer {
<router-outlet></router-outlet>
}
@placeholder {
<h1>Waiting…</h1>
}
@loading {
<app-loading></app-loading>
}
@error {
<h1>Couldn't access this component</h1>
}
and the loading component it is shown only if i let outside @loading block
One of the best videos on Deferrable Views, thank you! 😊😊
This was awesome! Thank you, Dmytro.
What the game changer, that's awesome feature
I like this feature, but I am worried that Angular templates will soon start looking like php wordpress template with a lot of logic embedded in the views.
on interaction is also a great feature!