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組件的名稱
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項(xiàng)目三級(jí)聯(lián)動(dòng)路由跳轉(zhuǎn)與傳參的思路詳解
這篇文章主要介紹了Vue項(xiàng)目三級(jí)聯(lián)動(dòng)的路由跳轉(zhuǎn)與傳參的思路詳解,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
VueJS組件之間通過(guò)props交互及驗(yàn)證的方式
本篇文章主要介紹了VueJS組件之間通過(guò)props交互及驗(yàn)證的方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-09-09
淺談vue中改elementUI默認(rèn)樣式引發(fā)的static與assets的區(qū)別
下面小編就為大家分享一篇淺談vue中改elementUI默認(rèn)樣式引發(fā)的static 與assets的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
前端報(bào)錯(cuò)npm ERR! cb() never called!問(wèn)題解決辦法
最近接手了一個(gè)前臺(tái)項(xiàng)目,執(zhí)行npm install的時(shí)候一直報(bào)錯(cuò),所以這里就給大家總結(jié)下,這篇文章主要給給大家介紹了關(guān)于前端報(bào)錯(cuò)npm?ERR! cb() never called!問(wèn)題的解決辦法,需要的朋友可以參考下2024-05-05
vue實(shí)現(xiàn)todolist單頁(yè)面應(yīng)用
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)todolist單頁(yè)面應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
vue2項(xiàng)目使用exceljs多表頭導(dǎo)出功能詳解
ExcelJS是一個(gè)用于在Node.js和瀏覽器中創(chuàng)建、讀取和修改Excel文件的強(qiáng)大JavaScript庫(kù),下面這篇文章主要給大家介紹了關(guān)于vue2項(xiàng)目使用exceljs多表頭導(dǎo)出功能的相關(guān)資料,需要的朋友可以參考下2024-05-05
Vue.js實(shí)現(xiàn)點(diǎn)擊左右按鈕圖片切換
這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)點(diǎn)擊左右按鈕圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
vue使用$emit時(shí),父組件無(wú)法監(jiān)聽(tīng)到子組件的事件實(shí)例
下面小編就為大家分享一篇vue使用$emit時(shí),父組件無(wú)法監(jiān)聽(tīng)到子組件的事件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

