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>

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

相关推荐
x***13392 小时前
【MyBatisPlus】MyBatisPlus介绍与使用
android·前端·后端
z***75154 小时前
【Springboot3+vue3】从零到一搭建Springboot3+vue3前后端分离项目之后端环境搭建
android·前端·后端
fruge5 小时前
仿写优秀组件:还原 Element Plus 的 Dialog 弹窗核心逻辑
前端
an86950015 小时前
vue新建项目
前端·javascript·vue.js
w***95496 小时前
SQL美化器:sql-beautify安装与配置完全指南
android·前端·后端
夏日玲子6 小时前
【Redis】Redis常用命令合集
数据库·redis·缓存
顾安r7 小时前
11.22 脚本打包APP 排错指南
linux·服务器·开发语言·前端·flask
万邦科技Lafite7 小时前
1688图片搜索商品API接口(item_search_img)使用指南
java·前端·数据库·开放api·电商开放平台
yinuo8 小时前
网页也懂黑夜与白天:系统主题自动切换
前端
Coding_Doggy8 小时前
链盾shieldchain | 项目管理、DID操作、DID密钥更新消息定时提醒
java·服务器·前端