自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Vue 中使用defineAsyncComponent 延遲加載組件

開發(fā) 前端
使用 Vue 3 的 defineAsyncComponent 特性可以讓我們延遲加載組件。這意味著它們僅在需要時從服務器加載。

[[414540]]

使用 Vue 3 的 defineAsyncComponent 特性可以讓我們延遲加載組件。這意味著它們僅在需要時從服務器加載。

這是改善初始頁面加載的好方法,因為我們的應用程序?qū)⒁暂^小的塊加載,而不必在頁面加載時加載每個組件。

在本教程中,我們將學習 defineAsyncComponent 的全部內(nèi)容,并看一個例子,該例子將一個彈出窗口的加載推遲到我們的應用程序需要的時候。

好了,讓我們開始吧。

什么是defineAsyncComponent

  1. // SOURCE: https://v3.vuejs.org/guide/component-dynamic-async.html 
  2. const AsyncComp = defineAsyncComponent( 
  3.   () => 
  4.     new Promise((resolve, reject) => { 
  5.       resolve({ 
  6.         template: '<div>I am async!</div>' 
  7.       }) 
  8.     }) 

defineAsyncComponent 接受一個返回Promise的工廠函數(shù)。當我們成功地從服務器獲取組件時,這個Promise應該會被 resolve ,如果出現(xiàn)錯誤則會被 reject 。

要使用它,我們必須從Vue中導入它,然后才能在腳本的其余部分中使用它。

我們也可以使用工廠函數(shù)中的 import ,輕松地從其他文件中添加Vue組件。

  1. import { defineAsyncComponent } from "vue"  
  2.  
  3. // 簡單使用 
  4. const LoginPopup = defineAsyncComponent(() => import("./components/LoginPopup.vue")) 

這是使用 defineAsyncComponent 的最簡單方法,但我們也可以傳入一個完整的選項對象,配置幾個更高級的參數(shù)。

  1. // with options  
  2. const AsyncPopup = defineAsyncComponent({  
  3.   loader: () => import("./LoginPopup.vue"), 
  4.   loadingComponent: LoadingComponent, /* 在加載時顯示 */ 
  5.   errorComponent: ErrorComponent, /* 顯示是否有錯誤 */ 
  6.   delay: 1000, /* 在顯示加載組件之前延遲毫秒 */ 
  7.   timeout: 3000 /* 這個毫秒之后的超時 */ 
  8. }) 

就我個人而言,我發(fā)現(xiàn)自己更經(jīng)常使用第一種較短的語法,它對我的大多數(shù)使用情況都有效,但這完全取決于你。

就這么簡單,讓我們進入我們的例子。

使用defineAsyncComponent延遲加載彈出組件

在本例中,我們將使用一個由單擊按鈕觸發(fā)的登錄彈出窗口。

每當我們的應用程序加載時,我們不需要我們的應用程序加載此組件,因為只有在用戶執(zhí)行特定操作時才需要它。

所以這就是我們的登錄組件的樣子,它只是通過用 position: fixed 將屏幕的其余部分涂黑來創(chuàng)建一個彈出窗口,并且有一些輸入和一個提交按鈕。

  1. <template> 
  2.    <div class="popup"
  3.        <div class="content"
  4.            <h4> Login to your account </h4> 
  5.            <input type="text" placeholder="Email" /> 
  6.            <input type="password" placeholder="Password" /> 
  7.            <button> Log in </button> 
  8.        </div> 
  9.    </div> 
  10. </template> 
  11.  
  12. <script> 
  13. </script> 
  14.  
  15. <style scoped> 
  16. .popup { 
  17.     position: fixed; 
  18.     width: 100%; 
  19.     top: ;  
  20.     left: ; 
  21.     height: 100%; 
  22.     background-color: rgba(, , , 0.2); 
  23.     display: flex; 
  24.     justify-content: center; 
  25.     align-items: center; 
  26. .content { 
  27.    min-width: 200px; 
  28.    width: 30%; 
  29.    background: #fff; 
  30.    height: 200px; 
  31.    padding: 10px; 
  32.    border-radius: 5px; 
  33. input[type="text"], input[type="password"] { 
  34.     border: ; 
  35.     outline: ; 
  36.     border-bottom: 1px solid #eee; 
  37.     width: 80%; 
  38.     margin:  auto; 
  39.     font-size: 0.5em; 
  40. button { 
  41.    border: ; 
  42.    margin-top: 50px; 
  43.    background-color:#8e44ad; 
  44.    color: #fff; 
  45.    padding: 5px 10px; 
  46.    font-size: 0.5em; 
  47. </style> 

而不是像我們通常那樣導入它并將其納入我們的 components 選項中。

  1. <!-- "Standard" way of doing things --> 
  2. <template> 
  3.   <button @click="show = true"> Login </button> 
  4.   <login-popup v-if="show" /> 
  5. </template> 
  6.  
  7. <script> 
  8. import LoginPopup from './components/LoginPopup.vue' 
  9. export default { 
  10.   components: { LoginPopup }, 
  11.   data() { 
  12.     return { 
  13.       show: false 
  14.     } 
  15.   } 
  16. </script> 

我們可以改為使用 defineAsyncComponent 僅在需要時加載它(意味著單擊按鈕并切換我們的 v-if)

  1. <!-- Use defineAsyncComponent  --> 
  2. <template> 
  3.   <button @click="show = true"> Login </button> 
  4.   <login-popup v-if="show" /> 
  5. </template> 
  6.  
  7. <script> 
  8. import { defineAsyncComponent } from 'vue' 
  9. export default { 
  10.   components: {  
  11.     "LoginPopup" : defineAsyncComponent(() => import('./components/LoginPopup.vue')) 
  12.   }, 
  13.   data() { 
  14.     return { 
  15.       show: false 
  16.     } 
  17.   } 
  18. </script> 

雖然這在我們使用我們的應用程序時可能看起來是一樣的,但讓我們檢查元素 > 網(wǎng)絡來理解這個小而重要的區(qū)別。

如果我們不使用 defineAsyncComponent,一旦我們的頁面加載,我們就會看到我們的應用程序從服務器上獲得LoginPopup.vue。雖然在這個例子中,這可能不是最大的性能問題,但它仍然會減慢加載速度,如果我們有幾十個組件這樣做,它真的會加起來。

但是,如果我們使用 defineAsyncComponent 查看同一個選項卡,我們會注意到當我們的頁面加載時,LoginPopup.vue 不見了,這是因為它還沒有加載。

但是一旦我們點擊我們的按鈕并告訴我們的應用程序顯示我們的彈出窗口,這時它就會從服務器加載,我們可以在網(wǎng)絡標簽中看到它。

這有助于我們實現(xiàn)最佳性能。我們只想在我們的頁面初始加載時加載需要的組件。有條件渲染的組件在我們的頁面加載時往往是不需要的,所以為什么要讓我們的應用程序加載它們呢?

如何使用異步設置功能

無論我們是否使用 defineAsyncComponent 延遲加載,任何具有異步設置功能的組件都必須用 包裝。

簡而言之,創(chuàng)建一個異步設置函數(shù)是我們的一個選擇,可以讓我們的組件在渲染前等待一些API調(diào)用或其他異步動作。

這是我們具有異步設置的組件。它使用 setTimeout() 模擬 API 調(diào)用。

  1. <template> 
  2.    <div class="popup"
  3.        <div class="content"
  4.             <p> Loaded API: {{ article }} </p> 
  5.            <h4> Login to your account </h4> 
  6.            <input type="text" placeholder="Email" /> 
  7.            <input type="password" placeholder="Password" /> 
  8.            <button> Log in </button> 
  9.        </div> 
  10.    </div> 
  11. </template> 
  12.  
  13. <script> 
  14. const getArticleInfo = async () => { 
  15.      // wait 3 seconds to mimic API call 
  16.     await new Promise(resolve => setTimeout(resolve, 1000)); 
  17.     const article = { 
  18.         title: 'My Vue 3 Article'
  19.         author: 'Matt Maribojoc' 
  20.     } 
  21.     return article 
  22. export default { 
  23.     async setup() { 
  24.         const article = await getArticleInfo() 
  25.         console.log(article) 
  26.         return { 
  27.             article 
  28.         } 
  29.     } 
  30. </script> 

我們可以在有或沒有 defineAsyncComponent 的情況下將它導入到我們的組件中

  1. import LoginPopup from './components/LoginPopup.vue' 
  2. // OR  
  3. const LoginPopup = defineAsyncComponent(() => import("./components/LoginPopup.vue")) 

但是如果我們想讓它在我們的模板中渲染,我們需要將它包裝在一個 Suspense 元素中。這將等待我們的 setup 函數(shù)在嘗試渲染我們的組件之前解析。

  1. <template> 
  2.   <button @click="show = true"> Login </button> 
  3.   <Suspense v-if="show"
  4.     <template #default
  5.       <login-popup  /> 
  6.     </template> 
  7.     <template #fallback> 
  8.       <p> Loading... </p> 
  9.     </template> 
  10.   </Suspense> 
  11. </template> 

這就是結(jié)果。用戶會看到 "正在加載......",然后在3秒后(我們的setTimeout的硬編碼值),我們的組件將渲染。

默認情況下,我們使用 defineAsyncComponent 定義的所有組件都是可暫停的。

這意味著如果一個組件的父鏈中有 Suspense,它將被視為該 Suspense 的一個異步依賴。我們的組件的加載、錯誤、延遲和超時選項將被忽略,而是由 Suspense 來處理。

最后的想法

defineAsyncComponent 在創(chuàng)建有幾十個組件的大型項目時是有好處的。當我們進入到懶惰加載組件時,我們可以有更快的頁面加載時間,改善用戶體驗,并最終提高你的應用程序的保留率和轉(zhuǎn)換率。

我想知道你對這個功能的看法。如果你已經(jīng)在你的應用中使用它了,請在下面的評論中告訴我。

原文:https://learnvue.co/2021/06/lazy-load-components-in-vue-with-defineasynccomponent/

作者:Matt Maribojoc

本文轉(zhuǎn)載自微信公眾號「前端全棧開發(fā)者」,可以通過以下二維碼關注。轉(zhuǎn)載本文請聯(lián)系前端全棧開發(fā)者公眾號。

 

責任編輯:武曉燕 來源: 前端全棧開發(fā)者
相關推薦

2024-08-13 09:26:07

2024-08-07 10:16:00

2021-09-29 11:33:19

異步組件Vue 3

2009-09-24 11:41:46

Hibernate延遲

2009-06-17 11:18:02

Hibernate延遲

2017-07-14 10:10:08

Vue.jsMixin

2012-08-15 11:36:13

Hibernate

2021-05-08 09:49:07

JavaScript延遲加載

2023-07-28 13:55:40

便捷選項組件

2009-09-28 09:56:53

Hibernate屬性

2024-10-15 07:42:09

Vue動態(tài)加載

2015-08-25 10:28:38

前端圖片延遲加載

2009-09-25 10:47:25

Hibernate延遲

2022-02-08 15:55:00

Vue組件庫Vue Demi

2020-02-10 10:23:03

VueJSX前端

2021-09-07 10:24:36

Vue應用程序Web Workers

2009-09-25 10:17:21

Hibernate延遲

2021-04-17 18:24:04

Vue.js嵌套路由前端

2020-08-04 11:35:38

Vue前端裝飾器

2024-02-05 13:07:00

.NETTable組件
點贊
收藏

51CTO技術棧公眾號