Angular 17 Introduces Deferrable Views as a New Feature

Posted by


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.

  1. 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
  1. 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>
  1. 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.

  1. 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.

0 0 votes
Article Rating
25 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@DecodedFrontend
30 days ago

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

@ankurmishra8543
30 days ago

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?

@ichr
30 days ago

Thank you very much, another really great explanation and use cases! 🙂

@atulkamble557
30 days ago

Beautifully explained! This is going to be very useful.

@XINTSUAI
30 days ago

How the @loading, @placeholder etc… knows which component they are waiting, since I can have multiple instances of the same "section"?

@rahulmukherjee4700
30 days ago

Fantastic explanation

@shubhamgoel7703
30 days ago

awesome feature thanks sensei 😍

@stickyamp5996
30 days ago

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.

@azelsky
30 days ago

what is the difference between "when" and "prefetch when"? Why do I need "prefetch when" if I could load content by just "when"?

@littleoddboy
30 days ago

no one could teach deferrable views in angular better.

@abdelhamidouanes9941
30 days ago

Bravo !! Big thanks <3 bien expliqué, j'apprécie beaucoup votre efforts <3

@MrVinodkumar92
30 days ago

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.

@aj.arunkumar
30 days ago

super awesome

@mitterkit
30 days ago

Fc from thailand

@eglimaja2549
30 days ago

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

@programandoprogramas-pp4565
30 days ago

One of the best videos on Deferrable Views, thank you! 😊😊

@GuillermoArellano
30 days ago

This was awesome! Thank you, Dmytro.

@AfifAlfiano
30 days ago

What the game changer, that's awesome feature

@danieldouglas210
30 days ago

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.

@MichielKikkert
30 days ago

on interaction is also a great feature!