vue中keep-alive組件實(shí)現(xiàn)多級(jí)嵌套路由的緩存
現(xiàn)狀(問(wèn)題):
keep-alive 組件對(duì)第三級(jí)及以上級(jí)的路由頁(yè)面緩存失效
探索方案:
方案1、直接將路由扁平化配置,都放在一級(jí)或二級(jí)路由中
方案2、再一層緩存組件用來(lái)過(guò)渡,并將其name配置到include中
實(shí)現(xiàn)方式
方案1不需要例子,按規(guī)則配置路由就行重點(diǎn)介紹方案2
因?yàn)槲矣昧藇ue-element-admin做了架構(gòu),并且項(xiàng)目中我將菜單和路由全部通過(guò)服務(wù)端返回做了統(tǒng)一配置,所以我只能用方案2來(lái)實(shí)現(xiàn)。
直接看原有代碼(問(wè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ù)樹(shù)形結(jié)構(gòu)做了處理(在store里寫(xiě)的,只展示出關(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) {
? ??? ?// 若有子級(jí) 先創(chuàng)建router-view容器,再去遞歸(重點(diǎn)重點(diǎn)重點(diǎn))
? ? ? tmp.component = {
? ? ? ??? ?render: c => c('router-view')
? ? ? }
? ? ? tmp.children = changeAsyncRoutes(tmp.children)
? ? } else {
? ? // 沒(méi)有子級(jí)菜單直接將component字符串解析成組件對(duì)象
? ? ? tmp.component = importMethod(tmp.component)
? ? }
? ? res.push(tmp)
? })
? return res
}這種寫(xiě)法已經(jīng)很完美了,可惜,我遇到了三級(jí)菜單不能緩存的問(wèn)題
直接上解決問(wèn)題的代碼
1、新建MenuMain.vue組件如下
// src/layout/component/MenuMain.vue
// 提供多級(jí)菜單的容器
<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ù)時(shí)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組件的名稱(chēng)
cachedViews: ['MenuMain']
到此這篇關(guān)于vue中keep-alive組件實(shí)現(xiàn)多級(jí)嵌套路由的緩存的文章就介紹到這了,更多相關(guān)vue keep-alive多級(jí)嵌套路由緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue中Keep-Alive緩存組件使用語(yǔ)法及原理深度解析
- Vue3除了keep-alive還有哪些實(shí)現(xiàn)頁(yè)面緩存詳解
- React實(shí)現(xiàn)頁(yè)面狀態(tài)緩存(keep-alive)的示例代碼
- Vue路由組件的緩存keep-alive和include屬性的具體使用
- Vue keep-alive組件的使用及如何清除緩存
- vue3?keep-alive實(shí)現(xiàn)tab頁(yè)面緩存功能
- Vue3嵌套路由中使用keep-alive緩存多層的實(shí)現(xiàn)
- vue使用keep-alive進(jìn)行組件緩存方法詳解(組件不緩存問(wèn)題解決)
- 快速解決 keep-alive 緩存組件中定時(shí)器干擾問(wèn)題
相關(guān)文章
解決vue多個(gè)路由共用一個(gè)頁(yè)面的問(wèn)題
下面小編就為大家分享一篇解決vue多個(gè)路由共用一個(gè)頁(yè)面的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
使用Vue3和Echarts?5繪制帶有立體感流線(xiàn)中國(guó)地圖(推薦收藏!)
最近接到一個(gè)需求是做一個(gè)中國(guó)地圖,下面這篇文章主要給大家介紹了關(guān)于如何使用Vue3和Echarts?5繪制帶有立體感流線(xiàn)中國(guó)地圖的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
vueJs函數(shù)readonly與shallowReadonly使用對(duì)比詳解
這篇文章主要為大家介紹了vueJs函數(shù)readonly與shallowReadonly使用對(duì)比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
vue3中使用VueParticles實(shí)現(xiàn)粒子動(dòng)態(tài)背景效果
為了提高頁(yè)面展示效果,特別類(lèi)似于登錄界面內(nèi)容比較單一的,粒子效果作為背景經(jīng)常使用到,vue工程中利用vue-particles可以很簡(jiǎn)單的實(shí)現(xiàn)頁(yè)面的粒子背景效果,本文給大家分享vue粒子動(dòng)態(tài)背景效果實(shí)現(xiàn)代碼,需要的朋友參考下吧2022-05-05
vue created鉤子函數(shù)與mounted鉤子函數(shù)的用法區(qū)別
這篇文章主要介紹了vue created鉤子函數(shù)與mounted鉤子函數(shù)的用法區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Vue使用JSEncrypt實(shí)現(xiàn)rsa加密及掛載方法
這篇文章主要介紹了Vue使用JSEncrypt實(shí)現(xiàn)rsa加密及掛載方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

