最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Vue3中實(shí)現(xiàn)緩存和復(fù)用動(dòng)態(tài)組件

 更新時(shí)間:2026年04月29日 16:52:12   作者:Astrifall  
本文介紹了在Vue3中使用<KeepAlive>組件和手動(dòng)管理組件實(shí)例來(lái)緩存和復(fù)用動(dòng)態(tài)組件的方法,使用<KeepAlive>可以緩存特定組件,控制緩存實(shí)例數(shù)量,并通過(guò)生命周期鉤子執(zhí)行相應(yīng)邏輯,手動(dòng)管理則通過(guò)對(duì)象存儲(chǔ)組件實(shí)例,提高性能,最后,將邏輯封裝到組合式函數(shù)中,增強(qiáng)代碼復(fù)用性

在 Vue 3 中,如何緩存和復(fù)用動(dòng)態(tài)組件,這有助于提高應(yīng)用的性能,避免組件重復(fù)創(chuàng)建和銷毀帶來(lái)的開(kāi)銷。

下面詳細(xì)介紹其使用方法和相關(guān)配置。

1. 使用<KeepAlive>組件緩存動(dòng)態(tài)組件

基本使用

<KeepAlive> 是 Vue 3 內(nèi)置的一個(gè)組件,它可以將包裹在其中的動(dòng)態(tài)組件進(jìn)行緩存,當(dāng)組件被切換隱藏時(shí),其狀態(tài)會(huì)被保留,再次顯示時(shí)無(wú)需重新創(chuàng)建。

<template>
  <div>
    <button @click="currentComponent = 'ComponentA'">顯示組件 A</button>
    <button @click="currentComponent = 'ComponentB'">顯示組件 B</button>
    <!-- 使用 KeepAlive 包裹動(dòng)態(tài)組件 -->
    <KeepAlive>
      <component :is="currentComponent" />
    </KeepAlive>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

const currentComponent = ref('ComponentA');

const components = {
  ComponentA,
  ComponentB
};
</script>

代碼解釋

  • 在模板中,使用 <KeepAlive> 組件包裹 <component :is="currentComponent" />,這樣 ComponentAComponentB 在切換時(shí)會(huì)被緩存。
  • currentComponent 是一個(gè)響應(yīng)式變量,用于控制顯示哪個(gè)組件。

包含和排除特定組件

你可以使用 includeexclude 屬性來(lái)指定哪些組件需要被緩存或排除。這兩個(gè)屬性的值可以是組件名稱的字符串、正則表達(dá)式或數(shù)組。

<template>
  <div>
    <button @click="currentComponent = 'ComponentA'">顯示組件 A</button>
    <button @click="currentComponent = 'ComponentB'">顯示組件 B</button>
    <!-- 只緩存 ComponentA -->
    <KeepAlive include="ComponentA">
      <component :is="currentComponent" />
    </KeepAlive>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

const currentComponent = ref('ComponentA');

const components = {
  ComponentA,
  ComponentB
};
</script>

代碼解釋

  • include="ComponentA" 表示只有 ComponentA 會(huì)被緩存,ComponentB 不會(huì)被緩存,每次切換到 ComponentB 時(shí)都會(huì)重新創(chuàng)建。
  • 若使用 exclude 屬性,則與之相反,被排除的組件不會(huì)被緩存。例如 <KeepAlive exclude="ComponentB"> 會(huì)緩存除 ComponentB 之外的組件。

最大緩存實(shí)例數(shù)

你可以使用 max 屬性來(lái)限制 <KeepAlive> 緩存的最大實(shí)例數(shù)量。當(dāng)緩存的實(shí)例數(shù)量超過(guò) max 值時(shí),最早緩存的實(shí)例會(huì)被銷毀。

<template>
  <div>
    <button @click="currentComponent = 'ComponentA'">顯示組件 A</button>
    <button @click="currentComponent = 'ComponentB'">顯示組件 B</button>
    <button @click="currentComponent = 'ComponentC'">顯示組件 C</button>
    <!-- 最多緩存 2 個(gè)組件實(shí)例 -->
    <KeepAlive max="2">
      <component :is="currentComponent" />
    </KeepAlive>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';
import ComponentC from './ComponentC.vue';

const currentComponent = ref('ComponentA');

const components = {
  ComponentA,
  ComponentB,
  ComponentC
};
</script>

代碼解釋

  • max="2" 表示 <KeepAlive> 最多緩存 2 個(gè)組件實(shí)例。當(dāng)切換到第三個(gè)組件時(shí),最早緩存的組件實(shí)例會(huì)被銷毀。

組件緩存狀態(tài)的生命周期鉤子

當(dāng)組件被 <KeepAlive> 緩存時(shí),會(huì)有兩個(gè)特殊的生命周期鉤子 onActivatedonDeactivated,可以在這兩個(gè)鉤子中執(zhí)行相應(yīng)的邏輯。

<template>
  <div>
    This is Component A.
  </div>
</template>

<script setup>
import { onActivated, onDeactivated } from 'vue';

onActivated(() => {
  console.log('Component A is activated.');
});

onDeactivated(() => {
  console.log('Component A is deactivated.');
});
</script>

代碼解釋

  • onActivated:當(dāng)組件被激活(從緩存中顯示出來(lái))時(shí)觸發(fā)。
  • onDeactivated:當(dāng)組件被停用(被隱藏并放入緩存)時(shí)觸發(fā)。

通過(guò)上述方法,你可以在 Vue 3 中靈活地緩存和復(fù)用動(dòng)態(tài)組件,提高應(yīng)用的性能和用戶體驗(yàn)。

2. 組件實(shí)例緩存(手動(dòng)管理)

除了使用 <KeepAlive>,你還可以手動(dòng)管理組件實(shí)例的緩存。通過(guò)一個(gè)對(duì)象來(lái)存儲(chǔ)組件實(shí)例,在需要使用時(shí)從對(duì)象中獲取。

<template>
  <div>
    <button @click="showComponent('ComponentA')">顯示組件 A</button>
    <button @click="showComponent('ComponentB')">顯示組件 B</button>
    <component :is="currentComponent" v-if="currentComponent" />
  </div>
</template>

<script setup>
import { ref, shallowRef, markRaw } from 'vue';
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

// 存儲(chǔ)組件實(shí)例的緩存對(duì)象
const componentCache = {};
const currentComponent = ref(null);

const showComponent = (name) => {
  if (!componentCache[name]) {
    const component = name === 'ComponentA' ? ComponentA : ComponentB;
    // 創(chuàng)建組件實(shí)例并存儲(chǔ)到緩存中
    componentCache[name] = markRaw(shallowRef(component));
  }
  currentComponent.value = componentCache[name].value;
};
</script>

代碼解釋

  • componentCache 是一個(gè)對(duì)象,用于存儲(chǔ)組件實(shí)例。
  • showComponent 方法根據(jù)傳入的組件名稱,檢查緩存中是否存在該組件實(shí)例。如果不存在,則創(chuàng)建實(shí)例并存儲(chǔ)到緩存中;如果存在,則直接從緩存中獲取。

3. 組合式函數(shù)封裝

可以將動(dòng)態(tài)組件的邏輯封裝到組合式函數(shù)中,提高代碼的復(fù)用性。

// useDynamicComponent.js
import { ref, shallowRef, markRaw } from 'vue';

export function useDynamicComponent() {
  const componentCache = {};
  const currentComponent = ref(null);

  const showComponent = (name, component) => {
    if (!componentCache[name]) {
      componentCache[name] = markRaw(shallowRef(component));
    }
    currentComponent.value = componentCache[name].value;
  };

  return {
    currentComponent,
    showComponent
  };
}
<template>
  <div>
    <button @click="showComponent('ComponentA', ComponentA)">顯示組件 A</button>
    <button @click="showComponent('ComponentB', ComponentB)">顯示組件 B</button>
    <component :is="currentComponent" v-if="currentComponent" />
  </div>
</template>

<script setup>
import { useDynamicComponent } from './useDynamicComponent.js';
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

const { currentComponent, showComponent } = useDynamicComponent();
</script>

代碼解釋

  • useDynamicComponent 組合式函數(shù)封裝了動(dòng)態(tài)組件的緩存和顯示邏輯。
  • 在組件中使用該組合式函數(shù),通過(guò)調(diào)用 showComponent 方法來(lái)顯示不同的組件。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue3實(shí)現(xiàn)Element Plus表格的多選功能與條件操作

    Vue3實(shí)現(xiàn)Element Plus表格的多選功能與條件操作

    Element-plus是ElementUI的升級(jí)版,是一套基于vue2與vue3的桌面端組件庫(kù),它提供了豐富的組件幫助開(kāi)發(fā)人員快速構(gòu)建功能強(qiáng)大、風(fēng)格統(tǒng)一的頁(yè)面,本文給大家介紹了Vue3實(shí)現(xiàn)Element Plus表格的多選功能與條件操作,需要的朋友可以參考下
    2024-08-08
  • 在vue-cli中引入lodash.js并使用詳解

    在vue-cli中引入lodash.js并使用詳解

    今天小編就為大家分享一篇在vue-cli中引入lodash.js并使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Vue中使用v-model雙向數(shù)據(jù)綁定select、checked等多種表單元素的方法

    Vue中使用v-model雙向數(shù)據(jù)綁定select、checked等多種表單元素的方法

    ?v-model?指令可以用在表單?input、textarea?及?select?元素上創(chuàng)建雙向數(shù)據(jù)綁定,它會(huì)根據(jù)控件類型自動(dòng)選取正確的方法來(lái)更新元素,本文給大家介紹Vue中如何使用v-model雙向數(shù)據(jù)綁定select、checked等多種表單元素,感興趣的朋友一起看看吧
    2023-10-10
  • vant中的picker選擇器自定義選項(xiàng)內(nèi)容

    vant中的picker選擇器自定義選項(xiàng)內(nèi)容

    這篇文章主要介紹了vant中的picker選擇器自定義選項(xiàng)內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • ElementUi中select框在頁(yè)面滾動(dòng)時(shí)el-option超出元素區(qū)域的問(wèn)題解決

    ElementUi中select框在頁(yè)面滾動(dòng)時(shí)el-option超出元素區(qū)域的問(wèn)題解決

    本文主要介紹了ElementUi中select框在頁(yè)面滾動(dòng)時(shí)el-option超出元素區(qū)域的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • vue cli 3.x 項(xiàng)目部署到 github pages的方法

    vue cli 3.x 項(xiàng)目部署到 github pages的方法

    這篇文章主要介紹了vue cli 3.x 項(xiàng)目部署到 github pages的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-04-04
  • Vue3解決Mockjs引入后并訪問(wèn)404(Not Found) 的頁(yè)面報(bào)錯(cuò)問(wèn)題

    Vue3解決Mockjs引入后并訪問(wèn)404(Not Found) 的頁(yè)面報(bào)錯(cuò)問(wèn)題

    mock.js:是一款模擬數(shù)據(jù)生成器,可以生成隨機(jī)數(shù)據(jù),攔截 Ajax 請(qǐng)求,使用mockjs模擬后端接口,可隨機(jī)生成所需數(shù)據(jù),模擬對(duì)數(shù)據(jù)的增刪改查,本文給大家介紹了Vue3解決Mockjs引入后并訪問(wèn)404(Not Found) 的頁(yè)面報(bào)錯(cuò)問(wèn)題,需要的朋友可以參考下
    2025-04-04
  • vue-json-viewer展示JSON內(nèi)容實(shí)踐

    vue-json-viewer展示JSON內(nèi)容實(shí)踐

    這篇文章主要介紹了vue-json-viewer展示JSON內(nèi)容實(shí)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • vite/Vuecli配置proxy代理解決跨域問(wèn)題

    vite/Vuecli配置proxy代理解決跨域問(wèn)題

    這篇文章主要介紹了vite/Vuecli配置proxy代理解決跨域問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • vue事件監(jiān)聽(tīng)函數(shù)on中的this指針域使用

    vue事件監(jiān)聽(tīng)函數(shù)on中的this指針域使用

    這篇文章主要介紹了vue事件監(jiān)聽(tīng)函數(shù)on中的this指針域使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論

苍南县| 大埔县| 新绛县| 隆昌县| 格尔木市| 社会| 虹口区| 新宁县| 墨脱县| 西宁市| 台中县| 西吉县| 麻江县| 清远市| 奉节县| 正蓝旗| 友谊县| 浦江县| 烟台市| 鄂托克前旗| 莱西市| 丹凤县| 正安县| 太谷县| 白朗县| 莲花县| 北京市| 江西省| 罗城| 巧家县| 濮阳县| 乌兰县| 昌江| 名山县| 沂源县| 信宜市| 黄浦区| 濮阳县| 怀宁县| 富蕴县| 齐河县|