Angular 中的新 @defer 功能是框架增強效能的一部分,特別是在延遲載入和渲染最佳化方面。以下是 @defer 功能以及 @placeholder 和 @loading 區塊的快速概述。
@defer { <large-component /> }
在底層,@defer 使用 IntersectionObserver API。此 API 可讓您觀察目標元素與祖先元素或頂級文件視窗的交集的變化。透過推遲組件的加載,直到它們即將進入視口,您可以避免加載用戶可能永遠不會看到的資源,從而節省頻寬和處理能力。
改進的初始載入時間:將元件推遲到需要時確保最初只載入應用程式的最關鍵部分。這減少了初始載入時間並提高了應用程式的感知效能,使其感覺更快、更靈敏。 Angular 將為延遲的元件建立單獨的包,從而減少主包的大小。
增強的使用者體驗:透過在內容可見之前載入內容,您可以確保為使用者提供更流暢、更無縫的體驗。此技術對於長滾動頁面特別有益,在用戶滾動時加載內容可以防止應用程式變得緩慢。
低功耗設備上的更好效能:處理能力有限或網路連線速度慢的裝置可以從延遲載入中顯著受益。透過僅加載必要的組件,這些設備可以更有效地處理應用程序,為各種設備上的用戶提供更好的體驗。
這是一個示範如何在 Angular 應用程式中使用 @defer 的範例。首先,建立一個載入圖像的元件。使用獨立組件是 @defer 的要求。
import { Component } from "@angular/core"; @Component({ selector: "app-images", standalone: true, template: `<div style="display: flex; flex-direction: column;"> @for(image of list; track image) { <img [src]="image" width="600" height="400" /> } </div> `, }) export class ImagesComponent { list = Array(5).fill("https://placehold.co/600x400"); }
這裡我們使用@defer,沒有任何選項。
<h1>Angular Defer Sample Application</h1> @defer () { <app-images></app-images> }
現在,查看生成的包,我們可以看到圖像組件有自己的塊。
在網路標籤中,當頁面載入時,我們可以看到該套件現在已在運行時載入。
有幾個選項可以增強使用者體驗。在本節中,我們將介紹其中的一些內容。
By default, the defer block will render the content when it is visible in the viewport. However, there can be delays, for example, when the components are making HTTP requests. In those scenarios, we can use the @placeholder option. The content used for the placeholder is not lazy loaded. The content in the @placeholder is shown first until the @defer block's contents are ready to render. The placeholder itself comes with an optional argument called minimum. This allows you to set the time to display the content.
Here is how this would look:
<h1>Angular Defer Sample Application</h1> @defer () { <app-images></app-images> } @placeholder (minimum 500ms) { <p>Loading Images</p> }
And here is how this looks:
The @loading block is used to display some content while the content defined in the @defer block is still loading or has started to load. This is different from the @placeholder block, which will appear before the loading starts. This block comes with two optional arguments, after and minimum. Similar to the @placeholder argument, the minimum argument is used to set the time to display the content. The second argument, after, is used to define the waiting time before showing the @loading content.
Here is how this would look:
<h1>Angular Defer Sample Application</h1> @defer () { <app-images></app-images> } @loading (after 1s; minimum 500ms) { <p>Loading Images</p> }
While you may not see this properly in the animated GIF, we are telling the block to wait at least 1 second before displaying the @loading content and show it for at least 500 ms.
The @defer feature in Angular is a powerful tool for enhancing performance and user experience by delaying the loading of components until they are needed. By integrating this feature with options like @placeholder and @loading, developers can create more efficient and responsive applications. As Angular continues to evolve, features like @defer will play a crucial role in optimizing resource management and improving application performance across various devices and network conditions.
以上是使用 @defer 和延遲載入提升角度效能的詳細內容。更多資訊請關注PHP中文網其他相關文章!