keep-alive动态缓存配置

vue项目中,列表进入详情返回列表需要缓存;但是点击菜单又不需要缓存,所以需要结合keep-alive设置页面动态缓存

  1. keep-alive设置includes缓存数组
<template>
  <router-view v-slot="{ Component }">
    <keep-alive :include="cachedPages">
      <component :is="Component" />
    </keep-alive>
  </router-view>
</template>

<script lang="ts">
import { computed, watch } from 'vue'
import { useStore } from 'vuex'
export default {
  setup() {
    let store = useStore()
    let cachedPages = computed(() => store.state.cachedPages)
    // watch(cachedPages, (newVal) => {
    //   console.log('[cache] keepAlive: ', newVal)
    // })
    return {
      cachedPages,
    }
  },
}
</script>
  1. store中的代码
import { createStore } from 'vuex'
export default createStore({
  state() {
    return {
      cachedPages: [], // keepAlive 页面
    }
  },
  mutations: {
    setCachedPages(state, pages) {
      state.cachedPages = pages
    },
  },
})
  1. 页面动态缓存代码
import { RouteLocationNormalized } from 'vue-router'
import store from '@/store'

export default function cachePage(route: RouteLocationNormalized) {
  //设置的页面缓存类型
  const cache = route.meta.cache
  //判断页面需不需要缓存
  if (cache) {
    const matched = route.matched
    //当前页面的组件名称
    const page = matched[matched.length - 1].components.default.name
    // console.log('[cache] current page: ', page)
    if (page) {
      let pages = [...store.state.cachedPages]
      // cache =pre 前置页面,类似于主菜单的页面,进入这个页面时,清空之前的缓存记录,然后保存当前打开的页面
      if (cache === 'pre') {
        // 前置页面
        pages = [page]
      } else {
       //非前置页面,直接根据,先判断页面之前有没有缓存过(主应用中页面多次跳转同一路由),有先删除,再缓存
        const index = pages.indexOf(page)
        if (index > -1) {
          pages = pages.slice(0, index)
        }
        pages.push(page)
      }
      // console.log('[cache] next pages: ', ...pages)
      store.commit('setCachedPages', pages)
    }
  }
}
  1. 页面router.js路由配置
const routes=[
	{
      path: 'componentList',
       name: 'componentList',
       component: componentList,
       meta: {
         auth: ['prvView'],
         cache: 'pre',
       },
     },
      // 构件包
      {
        path: 'package',
        redirect: {
          name: 'packageList',
        },
      },
      {
        path: 'package/index',
        name: 'packageList',
        component: packageList,
        meta: {
          auth: ['prvView'],
          cache: 'pre',//菜单页面,跳转的时候直接清空原来缓存,重新开始缓存页面
        },
      },
       // 加入构件包
      {
        path: 'package/:id/addComponent',
        name: 'packageAddComponent',
        component: PackageAddComponent,
        meta: {
          auth: ['prvView'],
          cache: true,//非菜单页面需要缓存的路由
        },
        props: (route) => ({ pkgId: route.params.id }),
      },
]
  1. 在beforeEach的时候调用cachePage函数
router.beforeEach(async (to, from) => {
  cachePage(to)
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值