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>

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

相关推荐
陈随易1 天前
编程语言级别的Skill市场,AI Agent 的未来形态
前端·后端·程序员
SoaringHeart1 天前
Flutter进阶:基于 EasyRefresh 的下拉刷新封装 n_easy_refresh_mixin.dart
前端·flutter
IT_陈寒1 天前
Vite的热更新突然不香了,排查三小时差点砸键盘
前端·人工智能·后端
子兮曰1 天前
Agency-Agents 深度解析:400+ AI 专家的"梦之队"如何重塑开发工作流
前端·后端·vibecoding
竹林8181 天前
用 The Graph 查询链上数据实战:从手搓 RPC 到 Subgraph,我的 NFT 项目数据加载快了 10 倍
前端·javascript
妙码生花1 天前
从 PHP 到 AI + Golang,程序员自救转型手记(十九):点选验证码代码逐行目检
前端·后端·go
Awu12271 天前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude
咪库咪库咪1 天前
Vue3-生命周期
前端
莪_幻尘1 天前
你的 AI Skill 越多越蠢?Token 上下文爆炸的求生指南
前端·ai编程
lichenyang4531 天前
从 has.echo 到异步 API 注册表:一次 ASCF API 回调不触发的排查复盘
前端