vue-element-admin 三四级路由菜单无法缓存 数据丢失问题 最简单的解决办法

vue-element-admin 开源框架,支持缓存的只能是一、二级路由页面,三级以上网上就无法缓存了;
结果就是在Tab页之间切换的时候,三级路由的页面会丢失数据,因为没有缓存,每次都是重新创建。

看了很多大佬的解决办法文章,尝试了很多,都无法轻松解决,折腾了很久,快要放弃的时候,想到一个笨办法,但是却很简单、很有效!!!

方法核心要点
1、将原本的三级路由页面 定义在一级 或 二级路由位置上; ----保证了缓存
2、将上述目标路由页面设置为隐藏;
3、在三级路由上定义一个页面,点击后,关闭当前页面,并跳转至目标页面(即1定义的页面);----保证了菜单层级效果

贴一下已经实现的代码,仅供参考

路由文件代码:

import Layout from '@/layout'

const thirdRouter = {
  // 一级路由
  path: '/first',
  component: Layout,
  redirect: '/first/third',
  name: 'FirstPage',
  meta: {
    title: 'FirstPage',
    icon: 'table'
  },
  // 二级路由
  children: [
    {
      path: 'second',
      component: () => import('@/views/xxxx/blank'),//有三级路由时这里是空组件
      name: 'SecondPage',
      meta: { title: 'SecondPage' },
      //三级路由
      children: [
        { // 过渡跳转页
          path: 'jump',
          component: () => import('@/views/xxxx/jumpto'), // 一个跳转页面
          name: 'JumpPage',
          meta: { title: 'ThirdPage' } //保持和目标页面相同标题
        }
      ]
    },
    { // 目标页面,注意:定义在二级路由位置,不是三级路由
      path: 'third',
      component: () => import('@/views/xxxx/third'),
      name: 'ThirdPage',
      meta: { title: 'ThirdPage' },
      hidden: true // 设置在左侧边栏隐藏,不影响右侧实际展示
    }
  ]
}
export default thirdRouter

跳转页代码
jumpto.vue

<template>
  <div title="jumpto">
    <router-view />
  </div>
</template>

<script>
export default {
  // 创建页面时,即调用jump方法
  created() {
    this.jump()
  },
  methods: {
    jump() {
      // 调用全局挂载的方法,关闭当前页
      this.$store.dispatch('tagsView/delView', this.$route)
      // 跳转至目标页面
      this.$router.push({
        name: 'ThirdPage'  //这里也可以使用path指定:path: '/first/third'
      })
    }
  }
}
</script>

二级空白组件
blank.vue

<template>
  <div title="blank">
    <router-view />
  </div>
</template>

这个方法很简单,实现效果就是 左侧边栏显示的是三级菜单,点击三级菜单(jumpto)后,跳转至目标页面(third),且缓存正常,因为目标页面本来就是1/2级的路由页面。

这是一个曲线救国的方法,但是简单、有效。适合前端新手,大佬可跳过。

注意:如果上述设置后还是出现没有缓存的,需要检查:路由配置中的 name参数 和 对应vue页面文件中的name参数是否一致?

路由中的:
在这里插入图片描述
vue页面文件中的:
在这里插入图片描述

vue-element-admin 中,可以通过配置路由 meta 中的 keepAlive 属性来进行缓存。但是对于路由缓存,需要将他们作为二级路由路由进行配置。 具体配置方式如下: 1. 在 router/index.js 中,将路由定义为二级路由路由,例如: ```javascript { path: '/system', component: Layout, redirect: '/system/menu', name: 'System', meta: { title: '系统管理', icon: 'example' }, children: [ { path: 'menu', name: 'Menu', component: () => import('@/views/system/menu'), meta: { title: '菜单管理', icon: 'table', keepAlive: true } }, { path: 'role', name: 'Role', component: () => import('@/views/system/role'), meta: { title: '角色管理', icon: 'tree', keepAlive: true } }, { path: 'user', name: 'User', component: () => import('@/views/system/user'), meta: { title: '用户管理', icon: 'user', keepAlive: true } }, { path: ':id', // 路由 name: 'EditUser', component: () => import('@/views/system/edit-user'), meta: { title: '编辑用户', icon: 'user', hidden: true } // 隐藏该路由 }, ] }, ``` 2. 在 src/layout/components/MultiTab下的index.vue中,监听路由变化,根据当前路由中的 fullPath 进行判断,如果是路由,则将 fullPath 的前缀作为 parentPath 保存下来,用于找到他的二级父路由,从而正确进行缓存。 ```javascript computed: { // 通过 fullPath 匹配出二级父路由名称 parentPath() { const { fullPath } = this.$route; const matched = this.$route.matched; if (matched && matched.length > 2) { const parentPath = `/${matched[1].path}`; return fullPath.replace(parentPath, '').split('/')[1]; } return ''; }, // 是否需要缓存 needKeepAlive() { const { meta } = this.$route; if (meta && (typeof meta.keepAlive !== 'undefined')) { return meta.keepAlive; } return false; } }, watch: { '$route': function(newVal, oldVal) { // 监听路由变化,判断是否需要缓存 if (this.needKeepAlive) { const fullPath = newVal.fullPath; if (fullPath !== oldVal.fullPath) { const parentPath = this.parentPath; if (parentPath) { this.cacheList.push(`${parentPath}/${fullPath}`); } else { this.cacheList.push(fullPath); } } this.$forceUpdate(); } } } ``` 3. 在 src/layout/components/MultiTab下的tab-list.vue中,根据传入的 cacheList 进行渲染路由缓存。 ```javascript <keep-alive> <router-view v-if="$route.meta.keepAlive" :key="$route.path" /> </keep-alive> <component v-else :is="currentRoute.component" :key="currentRoute.path" /> ... computed: { // 用于 keep-alive 的路由列表 keepAliaveList() { if (this.cacheList.length) { const keepAliveList = this.cacheList.map(item => { const matched = item.split('/'); if (matched.length > 2) { // 当前路由路由,需要找到他的二级父路由 const parentPath = `/${matched[1]}`; const parentRoute = this.getRouteObjByName(parentPath); if (parentRoute) { return { ...this.getRouteObjByName(item), parent: parentRoute }; } } else { // 当前路由是二级路由,直接返回 return this.getRouteObjByName(item); } }).filter(item => item); return keepAliveList; } return []; } }, methods: { // 根据路由名称获取路由对象 getRouteObjByName(name) { const routes = this.$router.options.routes; const obj = routes.find(route => route.path === name); return obj; }, // 关闭所有缓存的标签页 closeAllTags() { this.cacheList = []; this.$store.dispatch('multiTab/resetTabs'); }, ... } ``` 通过这些配置,即可实现 vue-element-admin路由缓存功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值