vue-element-admin 中多级菜单栏导致的路由缓存异常解决方案

问题:

当菜单栏左侧的层级超过2级就会导致keep-alive无法正常缓存路由,像这样

原因是打开三级页面<keep-alive include>中数组添加的是第三级页面的组件名,而页面实际展示的是

二级父页面包含的=>三级页面

而二级父页面组件名为加入到keep-alive 的 include数组中,导致超过2级的页面无法缓存

解决方案:

        方案1:

        最开始我以为只需要将二级父页面组件名加入到include的数组中就可以了,但是会引发新问题,就是像这种二级菜单下有多个同级的三级菜单,如果同时打开两个同级的三级菜单,关闭一个后,会导致另一个同级的三级菜单(页面之间的切换时)路由缓存失效,因为include中的数组把二级的组件名移除了,如果通过逻辑判断不移除,又会导致关闭页面后再次打开始终有缓存,所以该方案始终有瑕疵

        方案2:[最优解]

        考虑到二级菜单打开页面是没有父页面组件的,不存在缓存问题,于是转换思路把3级页面在不影响菜单结构的情况下复制一份到一级菜单下成为二级菜单,并设置hidden:true隐藏。

然后三级菜单设置 redirect="复制的二级菜单的path" ,也就是点击该三级菜单会跳转到他重定向的二级页面下,这样就能正常使用缓存了

hidden未设置true的情况下结构是这样的
hidden未设置为true的情况下结构是这样的

 

这样会有一个问题,那就是该栏不是选中时的蓝色,还需要再次点击下才会变为选中状态,所以需要修改SidebarItem.vue的代码

index属性原本是他的path路径改为在redirect存在的情况下,设置为redirect的路径

 

 这样点击第三级菜单两个都会是选中的颜色,然后再把下面的隐藏就完工了

 但是这样为了三级菜单去手动的配置路由太麻烦,只要需要在router.addRoutes(accessRoutes)前通过js完成三级的路由的拷贝,redirect,hidden设置就可以了,代码如下

        const accessRoutes = await store.dispatch('permission/generateRoutes', auths)
          // dynamically add accessible routes
          for (const index in accessRoutes) {
            if (accessRoutes[index].children){
              let temp=[]
              for (const child in accessRoutes[index].children) {
                const temp_child=accessRoutes[index].children[child]
                if (temp_child.children){
                  for (let i in temp_child.children){
                    const t2=temp_child.children[i]
                    const temp_child2={...t2}
                    temp_child2.hidden=true
                    temp.push(temp_child2)
                    accessRoutes[index].children[child].children[i]['redirect']=accessRoutes[index].path+'/'+t2.path
                  }
                }
              }
              accessRoutes[index].children=accessRoutes[index].children.concat(temp)
            }
          }
        router.addRoutes(accessRoutes)

 这样就完结了!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Vue-element-admin 是基于 Vue.js 和 Element UI 实现的后台管理系统框架。它采用了 Vue Router 实现路由跳转和页面切换。在 Vue-element-admin ,实现多级路由缓存可以通过以下步骤: 1. 在路由配置文件,设置需要缓存路由组件的 meta 属性添加 keepAlive: true。 ``` const routes = [ { path: '/', component: Layout, redirect: '/dashboard', children: [ { path: 'dashboard', component: () => import('@/views/dashboard/index'), name: 'Dashboard', meta: { title: 'Dashboard', icon: 'dashboard', keepAlive: true } } ] } ] ``` 2. 在 App.vue 文件,添加 keep-alive 组件,并设置 include 属性为需要缓存路由组件的名称。 ``` <template> <div id="app"> <keep-alive :include="cachedViews"> <router-view /> </keep-alive> </div> </template> <script> export default { name: 'App', computed: { cachedViews() { return this.$store.state.tagsView.cachedViews } } } </script> ``` 3. 在 store 文件夹创建 tagsView.js 文件,设置需要缓存路由组件的名称和缓存状态。 ``` const state = { cachedViews: [] } const mutations = { ADD_CACHED_VIEW: (state, view) => { if (state.cachedViews.includes(view.name)) return if (!view.meta.keepAlive) return state.cachedViews.push(view.name) }, DEL_CACHED_VIEW: (state, view) => { const index = state.cachedViews.indexOf(view.name) if (index > -1) { state.cachedViews.splice(index, 1) } } } const actions = { addCachedView({ commit }, view) { commit('ADD_CACHED_VIEW', view) }, delCachedView({ commit }, view) { commit('DEL_CACHED_VIEW', view) } } export default { namespaced: true, state, mutations, actions } ``` 这样就可以实现多级路由缓存了。需要注意的是,路由组件的名称必须是唯一的,否则可能会出现缓存错误。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值