vue中keep-alive組件實現(xiàn)多級嵌套路由的緩存
現(xiàn)狀(問題):
keep-alive 組件對第三級及以上級的路由頁面緩存失效
探索方案:
方案1、直接將路由扁平化配置,都放在一級或二級路由中
方案2、再一層緩存組件用來過渡,并將其name配置到include中
實現(xiàn)方式
方案1不需要例子,按規(guī)則配置路由就行重點介紹方案2
因為我用了vue-element-admin做了架構(gòu),并且項目中我將菜單和路由全部通過服務(wù)端返回做了統(tǒng)一配置,所以我只能用方案2來實現(xiàn)。
直接看原有代碼(問題代碼)
// src/layout/component/AppMain.vue
<template>
?<section class="app-main">
?? ? ?<transition name="fade-transform" mode="out-in">
?? ??? ? ? <keep-alive :include="cachedViews">
?? ??? ? ? ? ?? ?<router-view :key="key" />
?? ??? ? ? </keep-alive>
?? ? ?</transition>
?</section>
</template>
<script>
export default {
? name: 'AppMain',
? computed: {
? ? cachedViews() {
? ? ? return this.$store.state.tagsView.cachedViews
? ? },
? ? key() {
? ? ? return this.$route.path
? ? }
? }
}
</script>我從后端那到數(shù)據(jù)后,根據(jù)樹形結(jié)構(gòu)做了處理(在store里寫的,只展示出關(guān)鍵代碼)
// 拿到數(shù)據(jù)先循環(huán)第一層將Layout付給組件
?generateRoutes({ commit }, routeList) {
? ? return new Promise(resolve => {
? ? ? routeList.forEach(items => {
? ? ? ? items.component = Layout
? ? ? ? // 如果有子菜單直接再循環(huán)賦值
? ? ? ? items.children = changeAsyncRoutes(items.children)
? ? ? })
? ? ? commit('SET_ROUTES', routeList)
? ? ? resolve(routeList)
? ? })
? }
function changeAsyncRoutes(routes) {
? const res = []
? routes.forEach(route => {
? ? const tmp = { ...route }
? ? if (tmp.children && tmp.children.length !== 0) {
? ??? ?// 若有子級 先創(chuàng)建router-view容器,再去遞歸(重點重點重點)
? ? ? tmp.component = {
? ? ? ??? ?render: c => c('router-view')
? ? ? }
? ? ? tmp.children = changeAsyncRoutes(tmp.children)
? ? } else {
? ? // 沒有子級菜單直接將component字符串解析成組件對象
? ? ? tmp.component = importMethod(tmp.component)
? ? }
? ? res.push(tmp)
? })
? return res
}這種寫法已經(jīng)很完美了,可惜,我遇到了三級菜單不能緩存的問題
直接上解決問題的代碼
1、新建MenuMain.vue組件如下
// src/layout/component/MenuMain.vue
// 提供多級菜單的容器
<template>
? <keep-alive :include="cachedViews">
? ? <router-view :key="key" />
? </keep-alive>
</template>
<script>
export default {
? name: 'MenuMain', // 必須命名
? computed: {
? ? cachedViews() {
? ? ? return this.$store.state.tagsView.cachedViews
? ? },
? ? key() {
? ? ? return this.$route.path
? ? }
? }
}
</script>2、將此容器取代處理數(shù)據(jù)時render的 router-view 容器
// 引入組件
import MenuMain from '@/layout/components/MenuMain'
function changeAsyncRoutes(routes) {
? const res = []
? routes.forEach(route => {
? ? const tmp = { ...route }
? ? if (tmp.children && tmp.children.length !== 0) {
? ? // 注意看著里
? ? ? tmp.component = MenuMain
? ? ? // {
? ? ? // ? render: c => c('router-view')
? ? ? // }
? ? ? tmp.children = changeAsyncRoutes(tmp.children)
? ? } else {
? ? ? tmp.component = importMethod(tmp.component)
? ? }
? ? res.push(tmp)
? })
? return res
}3、把store中的 cachedViews 數(shù)組中始終保存MenuMain組件的名稱
cachedViews: ['MenuMain']
到此這篇關(guān)于vue中keep-alive組件實現(xiàn)多級嵌套路由的緩存的文章就介紹到這了,更多相關(guān)vue keep-alive多級嵌套路由緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Vue3和Echarts?5繪制帶有立體感流線中國地圖(推薦收藏!)
最近接到一個需求是做一個中國地圖,下面這篇文章主要給大家介紹了關(guān)于如何使用Vue3和Echarts?5繪制帶有立體感流線中國地圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
vueJs函數(shù)readonly與shallowReadonly使用對比詳解
這篇文章主要為大家介紹了vueJs函數(shù)readonly與shallowReadonly使用對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
vue3中使用VueParticles實現(xiàn)粒子動態(tài)背景效果
為了提高頁面展示效果,特別類似于登錄界面內(nèi)容比較單一的,粒子效果作為背景經(jīng)常使用到,vue工程中利用vue-particles可以很簡單的實現(xiàn)頁面的粒子背景效果,本文給大家分享vue粒子動態(tài)背景效果實現(xiàn)代碼,需要的朋友參考下吧2022-05-05
vue created鉤子函數(shù)與mounted鉤子函數(shù)的用法區(qū)別
這篇文章主要介紹了vue created鉤子函數(shù)與mounted鉤子函數(shù)的用法區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue使用JSEncrypt實現(xiàn)rsa加密及掛載方法
這篇文章主要介紹了Vue使用JSEncrypt實現(xiàn)rsa加密及掛載方法,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

