Vue3之組件狀態(tài)保持KeepAlive的簡單使用
作用說明
一個應(yīng)用場景
當我們在進行路由跳轉(zhuǎn)的時候,會用到 <router-view>
來作為 組件渲染的出口, 此時,組件的狀態(tài)是不會被保持的。
比如 : 當前在【組件A】中有一個響應(yīng)式狀態(tài) num
的值通過 自加的方式 從初始值0 變成了 100;
然后跳轉(zhuǎn)到 【組件B】,
再次跳轉(zhuǎn) 回到 【組件A】 的時候,num
的值會 恢復(fù)為初始值 0。
如何實現(xiàn) 回到 【組件A】的時候,仍然保持 num
的數(shù)據(jù)狀態(tài)為 100 呢?
這就是本文要介紹的 KeepAlive
組件的作用。
它就是用來做組件狀態(tài)保持的,或者叫做 緩存組件實例。
KeepAlive的簡單介紹
1、 直接使用 <keep-alive>
標簽,將目標組件包起來,就實現(xiàn)了組件的緩存;
2、配合路由的時候,需要使用到下面的格式 : 這個格式是固定的:
<router-view v-slot="{ Component }"> <keep-alive> <component :is="Component" /> </keep-alive> </router-view>
3、組件在緩存的時候,有兩個聲明周期鉤子可以配合使用:
onActivated
: 組件在被激活時觸發(fā)onDeactivated
: 組件在跳轉(zhuǎn)走被緩存時觸發(fā)
4、KeepAlive 有兩個屬性 :
include
: 指定要
緩存的組件exculde
: 指定不要
緩存的組件
以上兩個屬性 的值是 組件的名稱
, 可以是 數(shù)組的形式指定多個組件。
例如 : 有 兩個組件,name 分別是 a 和 b
- 例1 :
<keep-alive include="a">
- 例2 :
<keep-alive :include="['a','b']">
注意,當是數(shù)組的時候,需要使用 :
的形式。
代碼案例
本案例 有兩個普通的組件,分別包含了響應(yīng)式狀態(tài),
App.vue
中通過 按鈕 進行兩個組件的切換;
- 觀察 1:兩個組件的響應(yīng)式狀態(tài)是否保存了 ;
- 觀察2 :兩個組件的聲明周期鉤子。
本案例的項目結(jié)構(gòu)如下:
projectName | -- src | -- App.vue # 根組件 | -- componentA.vue # 組件A | -- componentB.vue # 組件B | -- main.ts # 程序入口文件 | -- router.ts # 路由配置文件 | -- index.html # 項目的入口文件
案例代碼
組件A componentA.vue
<template> <div class="diva"> 這是組件A <br> countNum : {{ countNum }} <br> <button @click="countNum++">+</button> <button @click="countNum--">-</button> </div> </template> <script setup lang="ts"> // 指定組件名稱 defineOptions({ name:'ca' }) import { ref,onMounted,onUnmounted,onActivated,onDeactivated} from 'vue' const countNum = ref(0) // 組件掛載成功后 onMounted(()=>{ console.log('組件A onMounted') }) // 組件卸載成功后 onUnmounted(() => { console.log('組件A onUnmounted') }), // 組件激活 onActivated(()=>{ console.log('組件A onActivated') }) // 組件失活 onDeactivated(()=>{ console.log('組件A onDeactivated') }) </script> <style scoped> .diva{ width: 300px; height: 200px; background: red; } </style>
組件B componentB.vue
<template> <div class="divb"> 這是組件B <br> message : {{ message }} <br> 輸入框 :<input type="text" v-model="message" /> </div> </template> <script setup lang="ts"> // 指定組件名稱 defineOptions({ name:'cb' }) import { ref,onMounted,onUnmounted,onActivated,onDeactivated} from 'vue' const message = ref('') // 組件掛載成功后 onMounted(()=>{ console.log('組件B onMounted') }) // 組件卸載成功后 onUnmounted(() => { console.log('組件B onUnmounted') }), // 組件激活 onActivated(()=>{ console.log('組件B onActivated') }) // 組件失活 onDeactivated(()=>{ console.log('組件B onDeactivated') }) </script> <style scoped> .divb{ width: 250px; height: 150px; background: green; } </style>
路由配置 router.ts
// 導(dǎo)入 定義路由的兩個方法 import {createRouter,createWebHistory} from 'vue-router' // 懶加載寫法 : 先聲明,下面直接使用 const componentA = () => import('./componentA.vue') const componentB = () => import('./componentB.vue') // 聲明路由跳轉(zhuǎn)的路徑與組件的對應(yīng)關(guān)系 const routsList = [ // 直接使用上面聲明的組件 {path:'/a',name:'aroute',component:componentA}, {path:'/b',name:'broute',component:componentB} ] // 創(chuàng)建路由的實例對象 const routerConfigObj = createRouter({ history:createWebHistory('abc'), // 帶一個參數(shù),表示是路由的一個前綴 routes:routsList // 指定路由的配置列表 }) // 導(dǎo)出路由的對象 export default routerConfigObj;
根組件 App.vue
<template> <div class="basediv"> APP.vue 中的 msg : {{ msg }} <br> <button @click="$router.push('/a')">跳轉(zhuǎn)到A</button> <button @click="$router.push('/b')">跳轉(zhuǎn)到B</button> <br> <!-- 核心 : keep-alive 的使用 --> <router-view v-slot="{ Component }"> <!-- :exclude="['ca','cb']" --> <keep-alive :include="['ca','cb']"> <component :is="Component" /> </keep-alive> </router-view> </div> </template> <script setup lang="ts"> // 引入 provide 方法 import { ref } from 'vue' // 聲明父組件的一個變量 const msg = ref('這是App根組件的msg變量') </script> <style scoped> .basediv{ width: 600px; height: 400px; border: 1px solid red; } </style>
運行結(jié)果
》1、初始狀態(tài) : 組件A中的數(shù)字執(zhí)行幾次自增
》2、點擊 跳轉(zhuǎn)到 組件B
點擊前 | 跳轉(zhuǎn)后 |
---|---|
![]() | ![]() |
》3、組件B中的輸入框輸入內(nèi)容
》4、點擊跳轉(zhuǎn)回 組件A
》5、再次跳轉(zhuǎn)回 組件B
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
element-ui表格合并span-method的實現(xiàn)方法
這篇文章主要介紹了element-ui表格合并span-method的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-05-05Vue計算屬性computed與方法methods的區(qū)別及說明
這篇文章主要介紹了Vue計算屬性computed與方法methods的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Vue+elementUI?el-input輸入框手機號校驗功能
這篇文章主要介紹了Vue+elementUI?el-input輸入框手機號校驗功能,限制input框內(nèi)只能輸入數(shù)字,且為11位,通過實例代碼介紹了對輸入手機號做校驗的方法,感興趣的朋友跟隨小編一起看看吧2023-10-10vue3之Suspense加載異步數(shù)據(jù)的使用
本文主要介紹了vue3之Suspense加載異步數(shù)據(jù)的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2023-02-02vue+element模態(tài)框中新增模態(tài)框和刪除功能
這篇文章主要介紹了vue+element模態(tài)框中新增模態(tài)框和刪除功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-06-06