Vue3动态路由KeepAlive设计方案(附详细代码)

 更新时间:2026年03月23日 08:24:39   作者:zmirror  
keep-alive是Vue内置的一个组件,可以使被包含的组件保留状态,或避免重新渲染,这篇文章主要介绍了Vue3动态路由KeepAlive设计方案的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下

1. 组件设置 name

核心:keep-alive 只认组件 name,所以 cachedViews 或 include 必须用组件 name,而不是路由 name。

1. defineComponent模式

<script lang="ts">
import { defineComponent} from 'vue';
export default defineComponent({
  name: 'CustomName'
})
</script>

2. setup语法糖

需要单独新增一个script标签

// 页面为 TSX 时,需将 <script> 标签改为 lang="tsx"
<script lang="ts">
  export default {
    name: 'CustomName',
    inheritAttrs: false,
    customOptions: {}
  }
</script>

Vue 3.3 中新引入了 defineOptions

defineOptions({
  name: 'CustomName',
  inheritAttrs: false,
  // ... 更多自定义属性
})

3. 借助插件

vue-macros 插件

2. 动态生成组件 name 的实践方案

相较于前端存储全量路由结合接口权限过滤的方案,此方案借助 Vue2 createCustomComponent 的思路:

  • 每条路由都会生成一个独立的组件对象,并为其分配唯一的 name
  • 在动态生成组件时,将组件的 name 设置为基于路由 path 处理后的安全名称

1. createCustomComponent

import { defineAsyncComponent, defineComponent, h } from "vue";

/**
 * @param {String} name 组件名称 (必须与 keep-alive 的 include 匹配)
 * @param {Function} componentLoader 即 () => import(...)
 */
export function createCustomComponent(name: string, componentLoader: any) {
  // 1. 检查 componentLoader 是否存在
  if (!componentLoader || typeof componentLoader !== "function") {
    console.error(`[路由错误]: 找不到组件 Name: ${name}`);
    // 返回一个同步的错误占位组件
    return defineComponent({ render: () => h("div", `组件 ${name} 加载失败`) });
  }
  // 2. 将 Vite 的加载函数包装成异步组件
  const AsyncComp = defineAsyncComponent({
    loader: componentLoader, // componentLoader 已经是 () => import(...)
    // 如果需要,可以在这里配置 loadingComponent
  });

  // 3. 直接返回渲染函数,渲染异步组件
  return defineComponent({
    name, // Keep-Alive 通过这个 name 识别缓存
    setup() {
      // 保持 AsyncComp 为直接子级
      return () => h(AsyncComp);
    },
  });
}

2. 组件名转化

// 将 path 转成合法的组件名,避免 '/' 等字符
function getComponentNameByPath(path: string) {
  return path.replace(/\//g, '-').replace(/^-/, '');
}

3. 路由接入示例

component: () => import("@/views/dashboard/index.vue")
// 调整为
component: createCustomComponent("Dashboard", import("@/views/dashboard/index.vue"))

3. 通用组件缓存策略

疑问:如果共用一个组件来进行创建、编辑、详情,怎么根据路径进行匹配?

假设路径是:/banner-list/banner-create/banner-list/banner-edit/banner-list/banner-detail 需要先进行路径命中匹配,无法命中则直接进行默认匹配:

  • 先解析上层路径,找到文件所在位置
  • 再进行精准匹配,比如:公共组件统一命名为:basic-component
// 扫描views目录下的vue文件
const modules = import.meta.glob("@/views/**/**.vue");
// 全局需要 keepAlive 的 path 列表
const keepAliveRoutes: string[] = [];
/**
* 解析后端返回的路由数据并转换为 Vue Router 兼容的路由配置
*
* @param rawRoutes 后端返回的原始路由数据
* @returns 解析后的路由配置数组
*/
const parseDynamicRoutes = (rawRoutes: RawRoute[]): RouteRecordRaw[] => {
  const parsedRoutes: RouteRecordRaw[] = [];

  rawRoutes.forEach(item => {
    const childrenColumn: RouteRecordRaw = {
      path: item.path,
      name: item.name,
      component: Layout,
      meta: {
        title: item.name,
        icon: item.icon || iconMap[item.path],
      },
      children: [] as RouteRecordRaw[],
    };
    if (item.children?.length) {
      childrenColumn.redirect = item.children[0].path;
      item.children.forEach(v => {
        childrenColumn.children.push({
          path: v.path,
          name: getComponentNameByPath(v.path),
          meta: {
            title: v.name,
            // 满足条件的path开启 keepAlive
            keepAlive: keepAliveRoutes.includes(v.path),
            // 取二级路由为高亮,兼容二、三级路由匹配
            activeMenu: v.path.match(/^/[^/]+/[^/]+/)?.[0],
          },
          component: createCustomComponent(
            getComponentNameByPath(v.path),
            modules[`/src/views${v.path}/index.vue`],
          ),
        });
      });
    }

    parsedRoutes.push(childrenColumn);
  });

  return parsedRoutes;
};

4. Vue2 createCustomComponent

// 将 path 转成合法的组件名,避免 '/' 等字符
function getComponentNameByPath(path) {
  return path.replace(/\//g, '-').replace(/^-/, '');
}

/**
 * @param {String} name 组件自定义名称
 * @param {Component | Promise<Component>} component
 */
export function createCustomComponent(name, component) {
  return {
    name,
    data() {
      return {
        // 这里的 component 指向解析后的组件对象
        component: null
      };
    },
    async created() {
      // 这里的 component 指向传入的参数(可能是 Promise)
      if (component instanceof Promise) {
        try {
          const res = await component;
          this.component = res.default || res;
        } catch (error) {
          console.error(`无法解析组件 ${name}:`, error);
        }
      } else {
        this.component = component;
      }
    },
    render(h) {
      if (!this.component) return null;
      // 只负责渲染组件,不传递任何东西
      return h(this.component);
    }
  };
}
// 调整路由文件获取
import NotFound from '@/components/NotFound';
const pagesContext = require.context('@/pages', true, /.vue$/);

function resolveComponent(path) {
  const directPath = `.${path}.vue`;
  const indexPath = `.${path}/index.vue`;

  let modulePath = null;
  if (pagesContext.keys().includes(directPath)) {
    modulePath = directPath;
  } else if (pagesContext.keys().includes(indexPath)) {
    modulePath = indexPath;
  }

  // 返回懒加载函数
  if (modulePath) {
    return () => Promise.resolve(pagesContext(modulePath).default);
  }

  // 找不到文件
  console.warn(`[router error] 页面未找到: ${path}`);
  return () => Promise.resolve({ render: h => h(NotFound, { props: { path } }) });
}

// 组件匹配示例代码
item.children.forEach(v => {
  childrenColumn.children.push({
    path: v.path,
    name: getComponentNameByPath(v.path),
    meta: {
      title: v.name,
      // 满足条件的path开启 keepAlive
      keepAlive: keepAliveRoutes.includes(v.path),
      // 取二级路由为高亮,兼容二、三级路由匹配
      activeMenu: v.activeMenu || v.path.match(/^\/[^/]+\/[^/]+/)?.[0]
    },
    component: createCustomComponent(getComponentNameByPath(v.path), resolveComponent(v.path))
  });
});

总结 

到此这篇关于Vue3动态路由KeepAlive设计方案的文章就介绍到这了,更多相关Vue3动态路由KeepAlive内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 在Linux服务器上部署vue项目

    在Linux服务器上部署vue项目

    这篇文章介绍了在Linux服务器上部署vue项目的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-11-11
  • vue使用JSON编辑器:vue-json-editor详解

    vue使用JSON编辑器:vue-json-editor详解

    文章介绍了如何在Vue项目中使用JSON编辑器插件`vue-json-editor`,包括安装、引入、注册和使用示例,通过这些步骤,用户可以在Vue应用中轻松实现JSON数据的编辑功能,文章最后呼吁大家支持脚本之家
    2025-01-01
  • vue单页应用的内存泄露定位和修复问题小结

    vue单页应用的内存泄露定位和修复问题小结

    系统进程不再用到的内存,没有及时释放,就叫做内存泄漏(memory leak)。这篇文章主要介绍了vue单页应用的内存泄露定位和修复,需要的朋友可以参考下
    2019-08-08
  • vue和react的区别及优缺点解读

    vue和react的区别及优缺点解读

    这篇文章主要介绍了vue和react的区别及优缺点说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • vue devserver及其配置方法

    vue devserver及其配置方法

    这篇文章主要介绍了vue devserver及其配置方法,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • 前端之vue3使用WebSocket的详细步骤

    前端之vue3使用WebSocket的详细步骤

    websocket实现的全双工通信,真真太香了,下面这篇文章主要给大家介绍了关于前端之vue3使用WebSocket的详细步骤,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • 前端Vue3+vant实现OSS上传图片全过程

    前端Vue3+vant实现OSS上传图片全过程

    这篇文章主要介绍了前端Vue3+vant实现OSS上传图片的相关资料,文中通过示例代码详细说明了上传文件的总流程,包括可能出现的跨域问题以及如何配置服务器以解决跨域问题,需要的朋友可以参考下
    2025-05-05
  • vue-cli构建的项目如何手动添加eslint配置

    vue-cli构建的项目如何手动添加eslint配置

    这篇文章主要介绍了vue-cli构建的项目如何手动添加eslint配置,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • 基于Vue实现微信小程序的图文编辑器

    基于Vue实现微信小程序的图文编辑器

    这篇文章主要介绍了基于Vue实现微信小程序的图文编辑器,由于微信小程序不能使用常规的图文编辑器(比如百度的UEditor )编辑新闻内容之类的,所以用vue写了个针对小程序用的图文编辑器需要的朋友可以参考下
    2018-07-07
  • Vue组件为什么data必须是一个函数

    Vue组件为什么data必须是一个函数

    这篇文章主要介绍了Vue组件为什么data必须是一个函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06

最新评论