其他分享
首页 > 其他分享> > 前端框架element-admin-vue keep-alive 三级菜单失效解决方案

前端框架element-admin-vue keep-alive 三级菜单失效解决方案

作者:互联网

出处:https://github.com/PanJiaChen/vue-element-admin/issues/406

1.解决keep-alive失效,在src目录下permission.js文件添加如下代码

//解决三级菜单keep-alive缓存失效
router.beforeResolve((to, from, next) => {
  if (to.matched && to.matched.length > 2) {
    to.matched.splice(1, to.matched.length - 2)
  }
  next();
})

 

  

  

2.解决面包屑失效

      

export const recursiveTreeByLastLevel = (val, data, fKey = 'value') => {
    let rData = [];
    for (let i = 0, len = data.length; i < len; i++) {
      rData.push(data[i]);
      if (data[i].children && data[i].children.length > 0) {
        rData = rData.concat(recursiveTreeByLastLevel(val, data[i].children, fKey));
        if (rData.some(item => item[fKey] === val)) return rData;
      }
      if (data[i][fKey] === val) return rData;
      rData = [];
    }
    return rData;
}

 

  

import { recursiveTreeByLastLevel } from '@/utils/recursiveTreeByLastLevel'

router.afterEach((to, from, next) => {
  var routerList = recursiveTreeByLastLevel(to.name, store.state.permission.routes, 'name')
  store.commit('permission/setBreadcrumbList', routerList) // 经过vuex缓存
  NProgress.done()
})

 

 

const mutations = {
  SET_ROUTES: (state, routes) => {
    // 所有角色都能访问的普通路由(login、404page) + 需要鉴权的路由
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
    },
    setBreadcrumbList:(state, routerList)=> {
    state.breadcrumbList = routerList
    }
}

 

  

  

 

 

 

标签:vue,admin,recursiveTreeByLastLevel,alive,item,state,rData,data,matched
来源: https://www.cnblogs.com/lyzi/p/16457976.html