VUE admin-element 后台管理系统三级菜单实现缓存

VUE admin-element 后台管理系统三级菜单实现缓存

框架无法直接实现三级菜单页面缓存,原因是由于直接缓存时没有把上级路由文件名称缓存进去,所以在框架基础上参考部分文章进行了一些改造

菜单文件,三级菜单引用文件路径修改,在nested下添加新文件src/views/nested/RouteLevelWrapper/index.vue

bash 复制代码
<template>
  <div class="app-main">
    <keep-alive :include="cachedViews">
      <router-view  />
    </keep-alive>
  </div>
</template>

<script>
export default {
  name: 'RouterViewKeepAlive',
  computed: {
    cachedViews() {
      return this.$store.state.tagsView.cachedViews
    },
    key() {
      return this.$route.path
    }
  }
}
</script>

tagsView.js文件中,增加多层路由缓存

bash 复制代码
  ADD_CACHED_VIEW: (state, view) => {
    if (state.cachedViews.includes(view.name)) return
    if (!view.meta.noCache) {
      //多层嵌套路由缓存问题处理
      if(view.matched.length>2){
        view.matched.forEach(item=>{
          if(item.name){
            state.cachedViews.push(item.name)
          }  
      })
    }else{
      state.cachedViews.push(view.name)
    }
  }
  },
    DEL_CACHED_VIEW: (state, view) => {
         //多层嵌套路由缓存问题处理
      if (view.matched && view.matched.length >= 3) { 
     state.cachedViews = state.cachedViews.filter(item => !view.matched.some(obj => obj.name === item));
      }  
    for (const i of state.cachedViews) {
      //多层嵌套路由缓存问题处理  
      if (i === view.name) {
        const index = state.cachedViews.indexOf(i)
        state.cachedViews.splice(index, 1)
        break
      }
    }
  },

需要缓存的页面名称与菜单配置组件名一致

AppMain 文件中

bash 复制代码
<template>
  <section class="app-main">
    <transition name="fade-transform" mode="out-in">
      <keep-alive :include="cachedViews">
        <router-view :key="key" />
      </keep-alive>
    </transition>
  </section>
</template>

<script>
export default {
  name: 'AppMain',
  computed: {
    cachedViews() {
      return this.$store.state.tagsView.cachedViews
    },
    key() {
      return this.$route.path
    }
  }
}
</script>

关键是将对应的菜单目录文件名称缓存到cachedViews数组

相关推荐
老刘说AI3 小时前
SGLang 深度优化: Radix 缓存与复杂任务的极致吞吐
人工智能·神经网络·机器学习·缓存·架构·sglang
m0_547486663 小时前
《Vue.js + uni-app全栈开发从入门到实践 》全套PPT课件2026版
vue.js·uni-app
AI分享猿4 小时前
重度长上下文开发怎么选?先看缓存是否吃额度
缓存·ai编程
向日的葵0064 小时前
Redis会话机制vsJWT机制深度解析
数据库·redis·python·缓存·系统架构·jwt
达子6665 小时前
第10章_HarmonyOs开发图解 用JS开发UI
vue.js
SEO_juper6 小时前
2026_GEO_AI搜索技术实战_CSDN
人工智能·chrome·目标检测·seo·geo·谷歌优化
Y3815326626 小时前
3 种 SERP 数据处理方案对比:流式 / 批处理 / 缓存
java·spring·缓存
奶糖 肥晨6 小时前
想return数据 返回的确是函数问题处理
vue.js
Database_Cool_6 小时前
LLM 应用怎么省 Token、加速响应——阿里云 Tair 语义缓存实战
阿里云·缓存·云计算
三言老师6 小时前
CentOS7.9:Redis 数据持久化结构化实战教程
数据库·redis·缓存