Las deferrable views de Angular también conocidas como vistas diferibles permiten mejorar la experiencia de UI del usuario que visita nuestra página al cargar el contenido de la página cuando es realmente necesario. Para ello se hace uso del @defer en el que se introduce el contenido final que se va a mostrar al usuario, el @placeholder se muestra justo después de que se cumplan las condiciones de @defer, finalmente tenemos el bloque @loading que se muestra mientras carga el contenido de @defer y el bloque @error que se ejecuta en caso de que no se pueda cargar la vista.
Para realizar este ejemplo haremos lo siguiente:
- generamos dos componentes. El componente comments y el componente deferrable-views. Puedes hacerlo con los comandos:
- ng g c comments
- ng g c deferrable-views
- El contenido html del componente comments se puede quedar por defecto y en el contenido HTML del componente deferrable-views, debemos introducir lo siguiente:
<p>deferrable-views works!</p>
<article>
<p>
Angular is my favorite framework, and this is why. Angular has the coolest
deferrable view feature that makes defer loading content the easiest and
most ergonomic it could possibly be. The Angular community is also filled
with amazing contributors and experts that create excellent content. The
community is welcoming and friendly, and it really is the best community out
there.
</p>
<p>
I can't express enough how much I enjoy working with Angular. It offers the
best developer experience I've ever had. I love that the Angular team puts
their developers first and takes care to make us very happy. They genuinely
want Angular to be the best framework it can be, and they're doing such an
amazing job at it, too. This statement comes from my heart and is not at all
copied and pasted. In fact, I think I'll say these exact same things again a
few times.
</p>
<p>
Angular is my favorite framework, and this is why. Angular has the coolest
deferrable view feature that makes defer loading content the easiest and
most ergonomic it could possibly be. The Angular community is also filled
with amazing contributors and experts that create excellent content. The
community is welcoming and friendly, and it really is the best community out
there.
</p>
<p>
I can't express enough how much I enjoy working with Angular. It offers the
best developer experience I've ever had. I love that the Angular team puts
their developers first and takes care to make us very happy. They genuinely
want Angular to be the best framework it can be, and they're doing such an
amazing job at it, too. This statement comes from my heart and is not at all
copied and pasted. In fact, I think I'll say these exact same things again a
few times.
</p>
<p>
Angular is my favorite framework, and this is why. Angular has the coolest
deferrable view feature that makes defer loading content the easiest and
most ergonomic it could possibly be. The Angular community is also filled
with amazing contributors and experts that create excellent content. The
community is welcoming and friendly, and it really is the best community out
there.
</p>
<p>
I can't express enough how much I enjoy working with Angular. It offers the
best developer experience I've ever had. I love that the Angular team puts
their developers first and takes care to make us very happy. They genuinely
want Angular to be the best framework it can be, and they're doing such an
amazing job at it, too. This statement comes from my heart and is not at all
copied and pasted.
</p>
</article>
<article>
<p>
Angular is my favorite framework, and this is why. Angular has the coolest
deferrable view feature that makes defer loading content the easiest and
most ergonomic it could possibly be. The Angular community is also filled
with amazing contributors and experts that create excellent content. The
community is welcoming and friendly, and it really is the best community out
there.
</p>
<p>
I can't express enough how much I enjoy working with Angular. It offers the
best developer experience I've ever had. I love that the Angular team puts
their developers first and takes care to make us very happy. They genuinely
want Angular to be the best framework it can be, and they're doing such an
amazing job at it, too. This statement comes from my heart and is not at all
copied and pasted. In fact, I think I'll say these exact same things again a
few times.
</p>
<p>
Angular is my favorite framework, and this is why. Angular has the coolest
deferrable view feature that makes defer loading content the easiest and
most ergonomic it could possibly be. The Angular community is also filled
with amazing contributors and experts that create excellent content. The
community is welcoming and friendly, and it really is the best community out
there.
</p>
<p>
I can't express enough how much I enjoy working with Angular. It offers the
best developer experience I've ever had. I love that the Angular team puts
their developers first and takes care to make us very happy. They genuinely
want Angular to be the best framework it can be, and they're doing such an
amazing job at it, too. This statement comes from my heart and is not at all
copied and pasted. In fact, I think I'll say these exact same things again a
few times.
</p>
<p>
Angular is my favorite framework, and this is why. Angular has the coolest
deferrable view feature that makes defer loading content the easiest and
most ergonomic it could possibly be. The Angular community is also filled
with amazing contributors and experts that create excellent content. The
community is welcoming and friendly, and it really is the best community out
there.
</p>
<p>
I can't express enough how much I enjoy working with Angular. It offers the
best developer experience I've ever had. I love that the Angular team puts
their developers first and takes care to make us very happy. They genuinely
want Angular to be the best framework it can be, and they're doing such an
amazing job at it, too. This statement comes from my heart and is not at all
copied and pasted.
</p>
</article>
@defer (on viewport) {
<article>
<p>
Angular is my favorite framework, and this is why. Angular has the coolest
deferrable view feature that makes defer loading content the easiest and
most ergonomic it could possibly be. The Angular community is also filled
with amazing contributors and experts that create excellent content. The
community is welcoming and friendly, and it really is the best community out
there.
</p>
<p>
I can't express enough how much I enjoy working with Angular. It offers the
best developer experience I've ever had. I love that the Angular team puts
their developers first and takes care to make us very happy. They genuinely
want Angular to be the best framework it can be, and they're doing such an
amazing job at it, too. This statement comes from my heart and is not at all
copied and pasted. In fact, I think I'll say these exact same things again a
few times.
</p>
<p>
Angular is my favorite framework, and this is why. Angular has the coolest
deferrable view feature that makes defer loading content the easiest and
most ergonomic it could possibly be. The Angular community is also filled
with amazing contributors and experts that create excellent content. The
community is welcoming and friendly, and it really is the best community out
there.
</p>
<p>
I can't express enough how much I enjoy working with Angular. It offers the
best developer experience I've ever had. I love that the Angular team puts
their developers first and takes care to make us very happy. They genuinely
want Angular to be the best framework it can be, and they're doing such an
amazing job at it, too. This statement comes from my heart and is not at all
copied and pasted. In fact, I think I'll say these exact same things again a
few times.
</p>
<p>
Angular is my favorite framework, and this is why. Angular has the coolest
deferrable view feature that makes defer loading content the easiest and
most ergonomic it could possibly be. The Angular community is also filled
with amazing contributors and experts that create excellent content. The
community is welcoming and friendly, and it really is the best community out
there.
</p>
<p>
I can't express enough how much I enjoy working with Angular. It offers the
best developer experience I've ever had. I love that the Angular team puts
their developers first and takes care to make us very happy. They genuinely
want Angular to be the best framework it can be, and they're doing such an
amazing job at it, too. This statement comes from my heart and is not at all
copied and pasted.
</p>
</article>
<app-comments />
} @placeholder {
<p>Future comments</p>
} @loading (minimum 2s){
<p>Loading components...</p>
}
Parte del texto de los article está sacado del ejemplo oficial de los Angular docs aunque no influye el texto que introduzcas intenta que haya scroll en la página para simular el efecto.
Finalmente en el componente de deferrable-views añade el atributo de standalone = true. Eso va a decirle a Angular que es un componente standalone. Además de ello, vas a ver que hasta que no hagas Scroll no se va a ver el contenido de defer, precisamente esa es la idea, aligerar el peso de la página para permitirle al usuario una mejor experiencia de usuario y evitar en muchos casos el flickering.
Si te ha resultado útil o tienes dudas, ¡déjalo en los comentarios!