vue-element-admin设置了页面缓存但无效的原因

检查组件中的name与路由中的name是否一致,只有一致的情况下有缓存。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 } ``` 这样就可以实现多级路由缓存了。需要注意的是,路由组件的名称必须是唯一的,否则可能会出现缓存错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值