詳細分析KeepAlive的基本知識 并緩存路由(附Demo)

目錄

  • 前言
  • 1. 基本知識
  • 2. Demo
    • 2.1 基本
    • 2.2 拓展
    • 2.3 終極
  • 3. 實戰

前言

🤟 找工作,來萬碼優才:👉 #小程序://萬碼優才/r6rqmzDaXpYkJZF

基本知識推薦閱讀:KeepAlive知識點

從實戰中學習,源自實戰中vue路由的緩存設置

<router-view v-if="routerAlive"><template #default="{ Component, route }"><keep-alive :include="getCaches"><component :is="Component" :key="route.fullPath" /></keep-alive></template>
</router-view>

截圖如下:

在這里插入圖片描述

1. 基本知識

<KeepAlive> 內置組件,用于緩存動態組件或路由組件,以提高性能
可以保留組件的狀態,避免重復渲染和生命周期鉤子的重新調用

KeepAlive 作用

  • 緩存組件,提高性能,避免組件反復銷毀和創建
  • 保留組件的狀態,例如表單填寫內容、滾動位置等
  • 控制緩存,可以指定哪些組件需要被緩存,哪些不需要

KeepAlive 適用場景

  • 需要緩存的 多頁面表單
  • 列表詳情頁切換 時,保留列表的滾動位置
  • 復雜組件切換時,避免重新渲染帶來的性能開銷
功能說明
KeepAlive用于緩存組件,提高性能
include指定要緩存的組件(支持字符串或數組)
exclude指定不緩存的組件
max限制最大緩存組件數量
activated()組件被激活時觸發
deactivated()組件被緩存時觸發

include 和 exclude
可以通過 include 和 exclude 來決定哪些組件需要被緩存,哪些不需要

<keep-alive include="ComponentA"><component :is="currentComponent"></component>
</keep-alive>

只有 ComponentA 會被緩存,而 ComponentB 不會

或者:

<keep-alive :include="['ComponentA', 'ComponentC']"><component :is="currentComponent"></component>
</keep-alive>

只有 ComponentA 和 ComponentC 會被緩存
如果需要排除某個組件:

<keep-alive exclude="ComponentB"><component :is="currentComponent"></component>
</keep-alive>

ComponentB 不會被緩存,而其他組件都會被緩存

max —— 設置緩存組件的最大數量
如果緩存的組件較多,可以設置 max 限制最多緩存多少個組件
只緩存最近的兩個組件,超出的組件會被銷毀

<keep-alive :max="2"><component :is="currentComponent"></component>
</keep-alive>

KeepAlive 生命周期鉤子
Vue 提供了兩個專門用于 KeepAlive 組件的生命周期鉤子:
activated():組件被激活(切換到緩存中的組件時調用)
deactivated():組件被緩存(切換到另一個組件時調用)

<script>
export default {created() {console.log("組件創建");},activated() {console.log("組件被激活");},deactivated() {console.log("組件被緩存");},destroyed() {console.log("組件被銷毀");},
};
</script>

運行效果:

  1. 組件首次渲染時,created() 會觸發
  2. 當組件切換回來時,activated() 會觸發,而不會重新執行 created()
  3. 切換到別的組件時,deactivated() 觸發,而不會執行 destroyed()
  4. 只有當緩存被清除時,才會執行 destroyed()

2. Demo

2.1 基本

動態組件緩存

<template><div><button @click="currentComponent = 'ComponentA'">切換到A</button><button @click="currentComponent = 'ComponentB'">切換到B</button><keep-alive><component :is="currentComponent"></component></keep-alive></div>
</template><script>
import ComponentA from "./ComponentA.vue";
import ComponentB from "./ComponentB.vue";export default {data() {return {currentComponent: "ComponentA",};},components: {ComponentA,ComponentB,},
};
</script>

組件 A (ComponentA.vue):

<template><div><h3>組件 A</h3><input v-model="text" placeholder="輸入內容會被緩存" /></div>
</template><script>
export default {data() {return {text: "",};},created() {console.log("ComponentA 創建");},destroyed() {console.log("ComponentA 被銷毀");},
};
</script>

組件 B (ComponentB.vue):

<template><div><h3>組件 B</h3></div>
</template><script>
export default {created() {console.log("ComponentB 創建");},destroyed() {console.log("ComponentB 被銷毀");},
};
</script>

一個最明顯的變化就是:
在 ComponentA 中輸入一些文字,然后切換到 ComponentB,再切回來,發現輸入內容還在(ComponentA 沒有被銷毀)

2.2 拓展

KeepAlive 也可以與 Vue Router 結合,緩存路由組件
這樣在 PageA 和 PageB 之間切換時,PageA 不會被銷毀,而是會被緩存

<template><div><router-link to="/pageA">頁面 A</router-link><router-link to="/pageB">頁面 B</router-link><keep-alive><router-view></router-view></keep-alive></div>
</template>

路由配置:

import { createRouter, createWebHistory } from "vue-router";
import PageA from "./PageA.vue";
import PageB from "./PageB.vue";const routes = [{ path: "/pageA", component: PageA },{ path: "/pageB", component: PageB },
];const router = createRouter({history: createWebHistory(),routes,
});export default router;

2.3 終極

這也是實戰中常用的一種方式,從實戰中抽離出基本的Demo,以 Vue 3 + Vue Router 4 + Pinia

裝依賴:npm install vue-router@4 pinia

main.ts(應用入口)

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import { createRouter, createWebHistory } from 'vue-router'
import App from './App.vue'
import Home from './views/Home.vue'
import About from './views/About.vue'
import Profile from './views/Profile.vue'const pinia = createPinia()// 定義路由
const routes = [{ path: '/', component: Home, name: 'Home' },{ path: '/about', component: About, name: 'About' },{ path: '/profile', component: Profile, name: 'Profile' }
]const router = createRouter({history: createWebHistory(),routes
})const app = createApp(App)
app.use(pinia)
app.use(router)
app.mount('#app')

store/tagsView.ts(Pinia 狀態管理 - 維護緩存組件列表)

import { defineStore } from 'pinia'export const useTagsViewStore = defineStore('tagsView', {state: () => ({cachedViews: new Set<string>()}),getters: {getCachedViews(): string[] {return Array.from(this.cachedViews)}},actions: {addCachedView(name: string) {this.cachedViews.add(name)},removeCachedView(name: string) {this.cachedViews.delete(name)},clearCachedViews() {this.cachedViews.clear()}}
})

App.vue(KeepAlive 組件封裝)

<script setup lang="ts">
import { computed, ref, provide, nextTick } from 'vue'
import { useTagsViewStore } from './store/tagsView'
import { RouterView } from 'vue-router'const tagsViewStore = useTagsViewStore()
const getCaches = computed(() => tagsViewStore.getCachedViews)// 無感刷新功能
const routerAlive = ref(true)
const reload = () => {routerAlive.value = falsenextTick(() => (routerAlive.value = true))
}
provide('reload', reload)
</script><template><div><router-view v-if="routerAlive"><template #default="{ Component, route }"><keep-alive :include="getCaches"><component :is="Component" :key="route.fullPath" /></keep-alive></template></router-view></div>
</template>

views/Home.vue

<script setup lang="ts">
import { inject } from 'vue'const reload = inject('reload') as () => void
</script><template><div><h1>Home Page</h1><button @click="reload">刷新當前組件</button></div>
</template>

views/About.vue

<template><div><h1>About Page</h1></div>
</template>

views/Profile.vue

<template><div><h1>Profile Page</h1></div>
</template>

動態路由的常用操作

  1. 動態添加路由
    在 router.ts 中可以動態添加路由:
import router from './router'const newRoute = {path: '/new-page',component: () => import('@/views/NewPage.vue'),name: 'NewPage'
}router.addRoute(newRoute)
  1. 動態移除路由:router.removeRoute('NewPage')

  2. 監聽路由變化

import { useRoute, useRouter } from 'vue-router'
import { watch } from 'vue'const route = useRoute()
const router = useRouter()watch(() => route.fullPath, (newPath) => {console.log('路由發生變化:', newPath)
})

3. 實戰

以ruoyi-vue-pro實戰的Demo進行講解,源碼:芋道源碼/ruoyi-vue-pro

具體KeepAlive,其文件在App.vue中

<router-view v-if="routerAlive"><template #default="{ Component, route }"><keep-alive :include="getCaches"><component :is="Component" :key="route.fullPath" /></keep-alive></template>
</router-view>

通過組件的設置是否路由進行緩存,后續獲取到這個路由信息時,對應判定是否該路由有緩存信息

const getCaches = computed((): string[] => {const caches = tagsViewStore.getCachedViewsconsole.log('當前緩存的組件:', caches) // 打印緩存的組件名稱return caches
})const tagsView = computed(() => appStore.getTagsView)//region 無感刷新
const routerAlive = ref(true)
// 無感刷新,防止出現頁面閃爍白屏
const reload = () => {routerAlive.value = falsenextTick(() => (routerAlive.value = true))
}
// 為組件后代提供刷新方法
provide('reload', reload)

對應的tagsView信息如下:
在這里插入圖片描述

后續在tsx中進行引用
在這里插入圖片描述

后續新增路由的時候,其路由地址 要和 defineOptions({ name: 'xxx' }) 對應一致

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/896938.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/896938.shtml
英文地址,請注明出處:http://en.pswp.cn/news/896938.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

記一次誤禁用USB導致鍵盤鼠標失靈的修復過程

背景說明 在電腦上插入了一個USB hub&#xff0c;然后彈窗提示&#xff1a;“集線器端口上出現電涌”&#xff0c;點開讓選擇“重置”或者“關閉”&#xff0c;不小心點了關閉&#xff0c;結果這個usb口就被關了&#xff0c;再插任何東西都沒反應&#xff0c;找了很多辦法都恢…

小米手機如何錄制屏幕?手機、電腦屏幕錄制方法分享

大家最近有沒有遇到想記錄手機屏幕操作的情況&#xff1f; 比如精彩的游戲瞬間、有趣的視頻教程&#xff0c;或者需要錄制屏幕來制作演示材料。小米手機在這方面可是個好幫手&#xff0c;今天就來給你好好嘮嘮&#xff0c;小米手機如何錄制屏幕&#xff0c;以及后續如何處理這…

如何將JAR交由Systemctl管理?

AI越來越火了&#xff0c;我們想要不被淘汰就得主動擁抱。推薦一個人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;最重要的屌圖甚多&#xff0c;忍不住分享一下給大家。點擊跳轉到網站 廢話不多說&#xff0c;進入正題。下面開始說如何使用 systemctl…

chrome Vue.js devtools 提示不支持該擴展組件,移除

可能是版本不兼容&#xff0c;可以重新安裝&#xff0c;推薦網址極簡插件官網_Chrome插件下載_Chrome瀏覽器應用商店 直接搜索vue&#xff0c;下載舊版&#xff0c;vue2、vue3都支持&#xff0c;上面那個最新版本試了下&#xff0c;vue2的肯定是不能用

【RabbitMQ】RabbitMQ的核心概念與七大工作模式

&#x1f525;個人主頁&#xff1a; 中草藥 &#x1f525;專欄&#xff1a;【中間件】企業級中間件剖析 在現代分布式系統和微服務架構中&#xff0c;消息隊列&#xff08;Message Queue&#xff09; 是解決服務間通信、系統解耦和流量削峰的關鍵技術之一。而 RabbitMQ 作為一…

SQLAlchemy系列教程:理解SQLAlchemy元數據

SQLAlchemy是Python開發人員的強大ORM工具。SQLAlchemy中的元數據是對象-關系映射配置的集合&#xff0c;允許開發人員無縫地定義和使用數據庫模式。 使用元數據 SQLAlchemy中的元數據充當各種數據庫描述符&#xff08;如表、列和索引&#xff09;的容器。這使開發人員能夠通…

MacDroid for Mac v2.3 安卓手機文件傳輸助手 支持M、Intel芯片 4.7K

MacDroid 是Mac毒搜集到的一款安卓手機文件傳輸助手&#xff0c;在Mac和Android設備之間傳輸文件。您只需要將安卓手機使用 USB 連接到 Mac 電腦上即可將安卓設備掛載為本地磁盤&#xff0c;就像編輯mac磁盤上的文件一樣編輯安卓設備上的文件&#xff0c;MacDroid支持所有 Andr…

Android+SpringBoot的老年人健康飲食小程序平臺

感興趣的可以先收藏起來&#xff0c;還有大家在畢設選題&#xff0c;項目以及論文編寫等相關問題都可以給我留言咨詢&#xff0c;我會一一回復&#xff0c;希望幫助更多的人。 系統介紹 我將從經濟、生活節奏、技術融合等方面入手&#xff0c;詳細闡述居家養老管理模式興起的…

【星云 Orbit ? STM32F4】10. 在串口接收中斷里即時解析數據頭的程序框架

【星云 Orbit ? STM32F4】10. 串口中斷中即時解析數據頭的程序開發&#xff1a;實現高效實時數據處理 摘要 在嵌入式開發中&#xff0c;串口中斷處理是實現高效實時數據傳輸的關鍵技術之一。本文將詳細介紹如何在STM32F407微控制器上開發一個在串口接收中斷中即時解析數據頭的…

數據倉庫的特點

數據倉庫的主要特點可以概括為&#xff1a;面向主題、集成性、非易失性、時變性、高性能和可擴展性、支持復雜查詢和分析、分層架構以及數據質量管理。 1. 面向主題&#xff08;Subject-Oriented&#xff09; 數據倉庫是面向主題的&#xff0c;而不是面向事務的。這意味著數據…

SAP MDG —— MDG on S/4HANA 2023 FPS03 創新匯總

文章目錄 MDG 基于SAP S/4HANA 2023 FPS03的創新BP/C/S&#xff1a;消息控制BP/C/S&#xff1a;手工分配數據控制者MDG-F&#xff1a;使用S/4擴展數據校驗功能生成式AI可用于協助自定義對象的數據變更/同時可總結批量變更的內容 MDG 基于SAP S/4HANA 2023 FPS03的創新 由于從S…

抽獎系統(從0-1)(上)

hu項目的開發流程介紹 1. 項目啟動階段 ? 項?概述&#xff1a;介紹項?的背景、?標和預期成果。 ? 團隊組建&#xff1a;建跨職能團隊&#xff0c;包括產品經理、UI/UX 設計師、開發?員、測試?員等。 ? ??定義&#xff1a;明確團隊中各個??的職責和?作內容。 2. 需…

vim 調整字體

vim: 在vim 面板單擊右鍵&#xff0c;選擇references: terminal :也是單擊右鍵,選擇references:

UniApp 使用 u-loadmore 完整步驟

文章目錄 一、前期準備1. 安裝 uView - UI 二、使用 u-loadmore組件1. 創建頁面2. 編寫頁面代碼模板部分&#xff08;loadmore-demo.vue&#xff09;樣式部分腳本部分 三、要點補充1. u-loadmore 狀態說明2. 數據請求優化3. 性能優化4. 兼容性問題 在 UniApp 開發中&#xff0c…

Libgdx游戲開發系列教程(3)——通過柏林噪音算法地圖隨機地形

在B站刷到了隨機地圖生成的視頻,隨手學習下并做下記錄 注: 本篇使用javafx應用作演示,算是了解這個算法的使用,后續會再出篇libgdx生成地圖的示例 說明 拋開算法實現,首先認知柏林噪音算法 一般我們想要隨機數,會指定個范圍,如0.0-1.0之間任意小數,而柏林算法的結果范圍就是[…

LeetCode熱題100JS(20/100)第四天|?41. 缺失的第一個正數?|?73. 矩陣置零?|?54. 螺旋矩陣?|?48. 旋轉圖像?

41. 缺失的第一個正數 題目鏈接&#xff1a;41. 缺失的第一個正數 難度&#xff1a;困難 刷題狀態&#xff1a;1刷 新知識&#xff1a; 解題過程 思考 示例 1&#xff1a; 輸入&#xff1a;nums [1,2,0] 輸出&#xff1a;3 解釋&#xff1a;范圍 [1,2] 中的數字都在數組中…

e2studio開發RA2E1(17)---- ADC掃描多通道采樣

e2studio開發RA2E1.17-- ADC掃描多通道采樣 概述視頻教學樣品申請硬件準備參考程序源碼下載ADC屬性配置回調函數主程序演示結果 概述 在嵌入式系統中&#xff0c;ADC&#xff08;模數轉換器&#xff09;是一個非常重要的組件&#xff0c;它將模擬信號轉換為數字信號。為了提高…

FPGA標準庫-Open Logic

在現代技術發展的浪潮中&#xff0c;開源項目已經成為了推動技術創新和發展的核心力量。無論是人工智能、區塊鏈、云計算&#xff0c;還是傳統的嵌入式開發、操作系統&#xff0c;開源項目都在其中扮演著至關重要的角色。它們不僅促進了技術的快速迭代&#xff0c;也為全球開發…

FineReport 操作注意

1.父單元格重復的時候&#xff0c;如何取消合并 效果如下&#xff1a; 只需要在單元格中&#xff0c;將數據設置為【列表】即可。 2.待定

開源之夏經驗分享|Koupleless 社區黃興抗:在開源中培養工程思維

開源之夏經驗分享&#xff5c;Koupleless 社區黃興抗&#xff1a;在開源中培養工程思維 文|黃興抗 電子信息工程專業 Koupleless 社區貢獻者 就讀于南昌師范學院&#xff0c;電子信息工程專業的大三學生。 本文 2634 字&#xff0c;預計閱讀 7? 分鐘? 今天 SOFAStack 邀…