vue中路由缓存

vue中路由缓存

问题描述及截图

在使用某一平台时发现当列表页码切换后点击某一卡片进入详情页后,再返回列表页时页面刷新了。这样用户每次看完详情回到列表页都得再重新输入自己的查询条件,或者切换分页到目标数据页等,操作起来非常闹心。


解决思路

在原有路由面包屑处理基础上,过滤路由数据与vue文件中的name一致后即可通过keep-alive组件的include属性缓存匹配到的路由。

关键代码及打印信息截图

bash 复制代码
修改文件:src\components\Layout\BasicLayoutPage.vue

<template>
	 <!-- 其他代码省略 -->
     <router-view v-slot="{ Component }">
          <keep-alive :include="cachedRoutes">
               <component :is="components || Component" />
          </keep-alive>
     </router-view>
</template>

<script setup lang="ts" name="BasicLayoutPage">
/**
 * cachedRoutes当前需要缓存的路由
 */
let cachedRoutes = ref<Record<string, any>>({});
/**
 * 面包屑处理
 */
const breadcrumbs = computed(() => {
    const paths = router.currentRoute.value.matched;
    let tempPaths = paths.map((item, index) => {
        return {
            index,
            isLast: index === paths.length - 1,
            path: item.path,
            breadcrumbName: (item.meta as any).title || '',
        };
    });
    console.log('tempPaths:', tempPaths);
    // 下面是处理要缓存的路由
    cachedRoutes.value = tempPaths.map((breadcrumb) =>
        breadcrumb.path.split('/').pop(), // 获取path中最后一个/后的值
    );
    console.log('cachedRoutes.value:', cachedRoutes.value);
    return tempPaths;
});
</script>

开发踩坑记,希望可以帮到正在解决该问题的你。若有侵权,联系立删。

相关推荐
qianmoQ10 分钟前
第五章:工程化实践 - 第三节 - Tailwind CSS 大型项目最佳实践
前端·css
尚学教辅学习资料19 分钟前
基于SpringBoot+vue+uniapp的智慧旅游小程序+LW示例参考
vue.js·spring boot·uni-app·旅游
椰果uu25 分钟前
前端八股万文总结——JS+ES6
前端·javascript·es6
微wx笑1 小时前
chrome扩展程序如何实现国际化
前端·chrome
~废弃回忆 �༄1 小时前
CSS中伪类选择器
前端·javascript·css·css中伪类选择器
CUIYD_19891 小时前
Chrome 浏览器(版本号49之后)‌解决跨域问题
前端·chrome
IT、木易1 小时前
跟着AI学vue第五章
前端·javascript·vue.js
guihong0041 小时前
Redis 深度解析:高性能缓存与分布式数据存储的核心利器
redis·分布式·缓存
薛定谔的猫-菜鸟程序员1 小时前
Vue 2全屏滚动动画实战:结合fullpage-vue与animate.css打造炫酷H5页面
前端·css·vue.js
春天姐姐2 小时前
vue3项目开发总结
前端·vue.js·git