keepalive路由缓存实现前进刷新后退缓存

1.在app.vue中配置全局的keepalive并用includes指定要缓存的组件路由name名字数组

<keep-alive :include="keepCachedViews">
      <router-view />
    </keep-alive>

computed: {
    keepCachedViews() {
      console.log('this.$store.getters.keepCachedViews', this.$store.getters.keepCachedViews)
      return this.$store.getters.keepCachedViews
    }
  },

2.在vuex中/store/module存储要缓存的组件路由数组

state: {
    keepCachedViews: []
  },
mutations: {
//增加方法
    UPDATE_KEEPCACHEDVIEWS: (state, view) => {
      console.log('增加缓存------', view)
      if (state.keepCachedViews.includes(view.name)) return
      if (!view.meta.noCache) {
        state.keepCachedViews.push(view.name)
      }
    },
//删除方法
    DELET_KEEPCACHEDVIEWS: (state, view) => {
      console.log('删除缓存------', view)
      const index = state.keepCachedViews.indexOf(view.name)
      index > -1 && state.keepCachedViews.splice(index, 1)
    },
},
actions: {
    updateKeepcachedViews({ commit }, views) {
      commit('UPDATE_KEEPCACHEDVIEWS', views)
    },
    deleteKeepcachedViews({ commit }, views) {
      commit('DELET_KEEPCACHEDVIEWS', views)
    },
}

3.在vuex getter.js中获取keepCachedViews

export default {
  keepCachedViews: state => state.ibps.app.keepCachedViews// 缓存的组件
}

4.在组件内守卫中判断什么时候缓存该组件

注意:路由离开时再添加缓存不生效 ,所以我想到的解决办法是在进入要缓存的页面前,先默认将页面添加到缓存数组中,在离开时再判断是否要缓存这个组件

ps:路由前置守卫中没有this,所以要用vm

  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.$store.dispatch('ibps/app/updateKeepcachedViews', to)
      return true
    })
  },
  beforeRouteLeave(to, from, next) {
    if (to.name != 'ylnlDataTemplateList') {
      this.$store.dispatch('ibps/app/deleteKeepcachedViews', from)
    }
    next()
  },

注意:路由中的name和组件的name要一致

### Vue3 中前进刷新后退时的页面缓存解决方案 #### 使用 `vcat-history` 插件 对于 Vue3 应用,推荐使用 `vcat-history` 轻量级插件来处理前进刷新后退时的页面缓存问题。该插件不仅支持 TypeScript 和 Vue3,而且能够提供平滑的过渡动画效果[^1]。 ```bash npm install vcat-history ``` 安装完成后,在项目的入口文件中引入并配置: ```javascript import { createApp } from 'vue'; import App from './App.vue'; import VCatHistory from 'vcat-history'; const app = createApp(App); app.use(VCatHistory, { cache: true, animation: true, }); app.mount('#app'); ``` #### 结合 `keep-alive` 组件优化缓存策略 为了更精细地控制哪些页面应该被缓存以及何时更新数据,可以在路由配置中结合 `keep-alive` 组件使用。这样不仅可以减少不必要的网络请求,还能提升用户体验。 定义需要缓存的视图组件: ```html <template> <div id="app"> <router-view v-slot="{ Component }"> <transition :name="$store.state.transitionName"> <component :is="Component" /> </transition> </router-view> </div> </template> <script setup lang="ts"> // ... </script> ``` 在路由配置里指定要缓存的具体路径: ```javascript { path: '/example', name: 'ExamplePage', component: ExampleComponent, meta: { keepAlive: true // 设置此属性表示允许缓存 } } ``` 当从详情页返回列表页时不希望加载新的数据,则可在目标组件内监听激活状态变化事件,并据此决定是否重载数据: ```typescript export default defineComponent({ data() { return { list: [], isUseCache: false, }; }, activated() { const fromRoute = this.$route.matched.find((record) => record.path === this.$route.meta.fromPath); if (fromRoute?.meta.noReload || !this.isUseCache) { this.list = []; this.fetchData(); } this.isUseCache = false; }, methods: { fetchData() { // 实现具体的数据拉取逻辑... }, }, }); ``` 针对某些特殊情况下的缓存失效现象(比如首次访问之后再次进入),可以通过检测当前实例的状态来进行适当调整。例如,在离开某个特定页面前保存其滚动位置或其他重要参数;下次再回到相同页面时恢复这些值以保持一致性的体验[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值