亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue中keep-alive多級(jí)路由緩存問題

 更新時(shí)間:2021年12月31日 11:10:50   作者:宇少_Lxuan  
本文主要介紹了vue中keep-alive多級(jí)路由緩存問題,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1.問題描述

對(duì)賬中心當(dāng)便捷導(dǎo)航菜單最后兩個(gè)是同一模塊且是三級(jí)及以上菜單時(shí),正常切換兩個(gè)便捷頁(yè)簽時(shí)是可以正常緩存的,但刪除最后一個(gè)頁(yè)簽時(shí),此時(shí)另一個(gè)頁(yè)簽頁(yè)面此時(shí)已經(jīng)不緩存了。

2.原因分析

keep-alive默認(rèn)支持緩存是兩級(jí),對(duì)三級(jí)及以上層級(jí)的頁(yè)面緩存失效,之前的處理方式為: 監(jiān)聽到路由變化后,將當(dāng)前的路由的標(biāo)識(shí)及父級(jí)標(biāo)識(shí)一起存起來,當(dāng)多個(gè)頁(yè)面存在時(shí),關(guān)閉其中一個(gè)頁(yè)面,也會(huì)將本身及父級(jí)的標(biāo)識(shí)一起刪掉,此時(shí)數(shù)組中已無父級(jí)標(biāo)識(shí),其他同級(jí)頁(yè)面的緩存將失效。

3.解決思路

將路由配置表的所有路由拆成兩個(gè)操作,一是保持原樣用于菜單的展示,二是對(duì)路由配置表進(jìn)行扁平化處理,將所有的路由處理成二級(jí)路由,這樣keep-alive就能默認(rèn)支持緩存了。

4.處理過程

拿到完整的路由配置

const modules = []
files.keys().forEach(key => {
    const filesObj = files(key).default || files(key)
    Object.keys(filesObj).forEach(keyOne => {
        modules.push(filesObj[keyOne])
    })
})

操作完成路由配置

export const menuList = modules;  // 用于菜單展示
?
const routerList = formatTwoStageRoutes(formatFlatteningRoutes(modules)); // 將路由扁平化為二級(jí)路由
?
const router = new VueRouter({
    scrollBehavior: () => ({ y: 0 }),
    mode: 'history',
    base: process.env.BASE_URL,
    routes: routerList, // 在路由配置項(xiàng)中使用扁平化處理后的路由
})

扁平化方法

export const formatFlatteningRoutes =(routesList => {
  if (routesList.length <= 0) return routesList;
  let list = [];
  routesList.forEach(v => {
    if(v.path === '/') {
      // 用于添加初試layout和首頁(yè),其他各中心配置過濾掉layout及父節(jié)點(diǎn),只保留children內(nèi)路由
      list.push(v);
      list = list.concat(formatFlatteningRoutes(v.children))
    } else if (v.children && v.children.length > 0) {
      list = list.concat(formatFlatteningRoutes(v.children))
    } else {
      list.push(v);
    }
  })
  return list;
})
?
export const formatTwoStageRoutes = list => {
  if (list.length <= 0) return list;
  const routerList = [];
  list.forEach(v => {
    if (v.path === '/') {
      routerList.push({
        component: v.component,
        name: v.name,
        path: v.path,
        redirect: v.redirect,
        meta: v.meta,
        children: []
      })
    } else if (v.path === '/login' || v.path === '/showcasePage') {
      // 不需要配置layout的頁(yè)面
      routerList.push(v)
    } else {
      routerList[0].children.push({ ...v })
    }
  })
  return routerList;
}

到此這篇關(guān)于vue中keep-alive多級(jí)路由緩存問題的文章就介紹到這了,更多相關(guān)vue keep-alive多級(jí)路由緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論