修复vue-element-admin多级路由keep-alive不生效问题
本次主要解决俩个问题:
1.多层次路由keep-alive失效
2.keep-alive生效后的后遗症(面包屑显示不全)
相信你都搜索到这里了就不需要太多废话了,直接上代码:
router/index文件:
router.beforeEach((to, from, next) => {
//修复多级路由不缓存
to.meta.matched = Object.assign([],to.matched)//面包屑
if (to.matched && to.matched.length > 2) {//目的是为了把多层次路由改为俩级
for (let i = 0; i < to.matched.length; i++) {
const element = to.matched[i]
if (!element.components.default.name) {//把中间没有对应实际组件的从matched中删掉
to.matched.splice(i, 1)
}
}
}
}
面包屑文件(Breadcrumb):
getBreadcrumb() {
let matched = this.$route.meta.matched.filter(item => {//这里做了改动,route.meta.matched
if (item.name) {
return true
}
})
const first = matched[0]
if (first && first.name !== '首页') {
matched = [{ path: '/home', meta: { title: '首页' }}].concat(matched)
}
this.levelList = matched
},
最后希望对大家有实际的帮助