搜尋
首頁web前端js教程使用 @defer 和延遲載入提升角度效能

介紹

Angular 中的新 @defer 功能是框架增強效能的一部分,特別是在延遲載入和渲染最佳化方面。以下是 @defer 功能以及 @placeholder 和 @loading 區塊的快速概述。

@defer 概述

目的

  • @defer 功能旨在延遲應用程式的元件或部分的載入和渲染,直到需要它們為止。這可以顯著改善應用程式的初始載入時間並增強用戶體驗。

用法

  • @defer 可以套用於模板的元件或部分,以指示它們應該延遲載入。這對於大型應用程式或應用程式的某些部分特別有用,這些應用程式或應用程式的某些部分在用戶首次登陸頁面時不會立即可見或不需要。

句法

  • 使用 @defer 的語法非常簡單,並且與 Angular 現有的模板語法無縫整合。以下是如何使用它的範例:
  @defer {
  <large-component></large-component>
  }

優點

  • 效能最佳化:透過推遲應用程式某些部分的加載,可以減少初始加載時間,從而帶來更快、響應更靈敏的用戶體驗。
  • 資源管理:延後元件的載入有助於更好的資源管理,因為資源僅在必要時使用。
  • 使用者體驗:它透過首先載入關鍵內容並推遲不太關鍵的內容直到需要時來增強使用者體驗。

與 Angular 生態系統集成

  • @defer 功能與其他 Angular 功能和工具整合良好,使開發人員能夠以一致的方式利用延遲載入、更改檢測策略和其他效能最佳化技術。

前景

  • 隨著 Angular 的不斷發展,@defer 功能可能會得到進一步的增強和最佳化。開發人員可以期望對其應用程式的某些部分的載入和渲染方式以及時間進行更精細的控制。

@defer 和 IntersectionObserver

在底層,@defer 使用 IntersectionObserver API。此 API 可讓您觀察目標元素與祖先元素或頂級文件視窗的交集的變化。透過推遲組件的加載,直到它們即將進入視口,您可以避免加載用戶可能永遠不會看到的資源,從而節省頻寬和處理能力。

IntersectionObserver 的其他好處

改進的初始載入時間:將元件推遲到需要時確保最初只載入應用程式的最關鍵部分。這減少了初始載入時間並提高了應用程式的感知效能,使其感覺更快、更靈敏。 Angular 將為延遲的元件建立單獨的包,從而減少主包的大小。

增強的使用者體驗:透過在內容可見之前載入內容,您可以確保為使用者提供更流暢、更無縫的體驗。此技術對於長滾動頁面特別有益,在用戶滾動時加載內容可以防止應用程式變得緩慢。

低功耗設備上的更好效能:處理能力有限或網路連線速度慢的裝置可以從延遲載入中顯著受益。透過僅加載必要的組件,這些設備可以更有效地處理應用程序,為各種設備上的用戶提供更好的體驗。

範例

在沒有任何選項的情況下使用@defer

這是一個示範如何在 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="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172288960334356.png?x-oss-process=image/resize,p_40" class="lazy"    style="max-width:90%"  style="max-width:90%" alt="使用 @defer 和延遲載入提升角度效能" >
    }
  </div> `,
})
export class ImagesComponent {
  list = Array(5).fill("https://placehold.co/600x400");
}

這裡我們使用@defer,沒有任何選項。

<h1 id="Angular-Defer-Sample-Application">Angular Defer Sample Application</h1>
@defer () {
<app-images></app-images>
}

現在,查看生成的包,我們可以看到圖像組件有自己的塊。

Boosting Angular Performance with @defer and Lazy Loading

在網路標籤中,當頁面載入時,我們可以看到該套件現在已在運行時載入。

Boosting Angular Performance with @defer and Lazy Loading

將 @defer 與選項一起使用

有幾個選項可以增強使用者體驗。在本節中,我們將介紹其中的一些內容。

Using @placeholder

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 id="Angular-Defer-Sample-Application">Angular Defer Sample Application</h1>
@defer () {
<app-images></app-images>
} @placeholder (minimum 500ms) {
<p>Loading Images</p>
}

And here is how this looks:

Boosting Angular Performance with @defer and Lazy Loading

Using @loading

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 id="Angular-Defer-Sample-Application">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.

Boosting Angular Performance with @defer and Lazy Loading

Conclusion

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中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在JavaScript中替換字符串字符在JavaScript中替換字符串字符Mar 11, 2025 am 12:07 AM

JavaScript字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

jQuery檢查日期是否有效jQuery檢查日期是否有效Mar 01, 2025 am 08:51 AM

簡單JavaScript函數用於檢查日期是否有效。 function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] '/' bits[1] '/' bits[0]); return !!(d && (d.getMonth() 1) == bits[1] && d.getDate() == Number(bits[0])); } //測試 var

jQuery獲取元素填充/保證金jQuery獲取元素填充/保證金Mar 01, 2025 am 08:53 AM

本文探討如何使用 jQuery 獲取和設置 DOM 元素的內邊距和外邊距值,特別是元素外邊距和內邊距的具體位置。雖然可以使用 CSS 設置元素的內邊距和外邊距,但獲取準確的值可能會比較棘手。 // 設定 $("div.header").css("margin","10px"); $("div.header").css("padding","10px"); 你可能會認為這段代碼很

10個jQuery手風琴選項卡10個jQuery手風琴選項卡Mar 01, 2025 am 01:34 AM

本文探討了十個特殊的jQuery選項卡和手風琴。 選項卡和手風琴之間的關鍵區別在於其內容面板的顯示和隱藏方式。讓我們深入研究這十個示例。 相關文章:10個jQuery選項卡插件

10值得檢查jQuery插件10值得檢查jQuery插件Mar 01, 2025 am 01:29 AM

發現十個傑出的jQuery插件,以提升您的網站的活力和視覺吸引力!這個精選的收藏品提供了不同的功能,從圖像動畫到交互式畫廊。讓我們探索這些強大的工具:相關文章:1

HTTP與節點和HTTP-Console調試HTTP與節點和HTTP-Console調試Mar 01, 2025 am 01:37 AM

HTTP-Console是一個節點模塊,可為您提供用於執行HTTP命令的命令行接口。不管您是否針對Web服務器,Web Serv

自定義Google搜索API設置教程自定義Google搜索API設置教程Mar 04, 2025 am 01:06 AM

本教程向您展示瞭如何將自定義的Google搜索API集成到您的博客或網站中,提供了比標準WordPress主題搜索功能更精緻的搜索體驗。 令人驚訝的是簡單!您將能夠將搜索限制為Y

jQuery添加捲軸到DivjQuery添加捲軸到DivMar 01, 2025 am 01:30 AM

當div內容超出容器元素區域時,以下jQuery代碼片段可用於添加滾動條。 (無演示,請直接複製到Firebug中) //D = document //W = window //$ = jQuery var contentArea = $(this), wintop = contentArea.scrollTop(), docheight = $(D).height(), winheight = $(W).height(), divheight = $('#c

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具