DYNAMIC_ROUTE_BREADCRUMB.md 5.75 KB

动态路由面包屑处理指南

概述

本文档说明如何在面包屑中正确处理动态路由(如 /assistant/edit/:id)并获取对应的菜单标题。

核心功能

系统已经支持以下功能:

  1. 动态路由匹配:isPathMatch 函数支持匹配包含参数的路由(如 /assistant/edit/:id 可以匹配 /assistant/edit/123)
  2. 菜单链查找:getMenuChain 函数可以查找动态路由对应的菜单项及其所有父级菜单
  3. 动态标题生成:支持根据路由参数动态生成面包屑标题

使用方式

方式一:在菜单配置中定义动态路由菜单项

在菜单配置中,可以直接定义包含动态参数的路径:

{
  id: 'assistant-edit',
  title: '编辑智能体',
  path: '/assistant/edit/:id',  // 注意:路径中包含 :id 参数
  meta: {
    showInBreadcrumb: true,
  }
}

当访问 /assistant/edit/123 时,系统会:

  1. 自动匹配到菜单配置中的 /assistant/edit/:id
  2. 在面包屑中显示"编辑智能体"
  3. 面包屑路径使用实际路径 /assistant/edit/123(而不是 /assistant/edit/:id)

方式二:使用路由元信息配置动态标题

如果菜单配置中没有对应的菜单项,可以在路由配置中使用 meta.title:

{
  path: '/assistant/edit/:id',
  name: 'AssistantEdit',
  component: () => import('@/views/Assistant/addUpdate.vue'),
  meta: {
    title: '编辑',  // 基础标题
    // 可选:自定义标题格式
    breadcrumbTitleFormat: '{title} #{param}',  // 例如:"编辑 #123"
  },
}

方式三:使用自定义标题生成函数

对于更复杂的场景,可以使用自定义函数生成标题:

{
  path: '/assistant/edit/:id',
  name: 'AssistantEdit',
  component: () => import('@/views/Assistant/addUpdate.vue'),
  meta: {
    title: '编辑',
    // 自定义标题生成函数
    breadcrumbTitleGenerator: (menuTitle, routeParams, routePath) => {
      const id = routeParams.id;
      // 可以根据 id 从 store 或其他地方获取详细信息
      // 例如:const agent = getAgentById(id);
      // return agent ? `编辑 ${agent.name}` : `${menuTitle} #${id}`;
      return `${menuTitle} #${id}`;
    },
  },
}

实现原理

1. 路径匹配算法

isPathMatch 函数使用正则表达式匹配动态路由:

// 将 /assistant/edit/:id 转换为正则表达式
// /assistant/edit/[^/]+
const pattern = normalizedMenuPath
  .replace(/:[^/]+/g, '[^/]+')
  .replace(/\//g, '\\/');
const regex = new RegExp(`^${pattern}$`);

2. 菜单链查找

getMenuChain 函数递归查找匹配的菜单项及其所有父级:

// 对于路径 /assistant/edit/123
// 会查找并返回:
// [
//   { title: '岗位助理配置', path: '/assistant' },
//   { title: '编辑智能体', path: '/assistant/edit/:id' }
// ]

3. 动态标题生成

getDynamicBreadcrumbTitle 函数根据以下优先级生成标题:

  1. 如果路由元信息中有 breadcrumbTitleGenerator 函数,使用该函数
  2. 如果路由元信息中有 breadcrumbTitleFormat 格式字符串,使用该格式
  3. 否则返回原始菜单标题

最佳实践

1. 菜单配置优先

推荐:在菜单配置中定义动态路由菜单项,这样可以:

  • 保持菜单和面包屑的一致性
  • 支持菜单高亮和展开
  • 更好的可维护性
// 在菜单配置中
{
  id: 'assistant-edit',
  title: '编辑智能体',
  path: '/assistant/edit/:id',
  parentId: 'assistant-list',
}

2. 路由元信息作为备选

如果菜单配置中没有对应的菜单项,使用路由元信息:

// 在路由配置中
{
  path: '/assistant/edit/:id',
  meta: {
    title: '编辑智能体',
  },
}

3. 复杂场景使用自定义函数

当需要根据路由参数获取详细信息时,使用自定义函数:

meta: {
  breadcrumbTitleGenerator: async (menuTitle, routeParams) => {
    const id = routeParams.id;
    // 从 API 或 store 获取详细信息
    const detail = await fetchAgentDetail(id);
    return detail ? `编辑 ${detail.name}` : menuTitle;
  },
}

示例场景

场景 1:编辑页面

路由:/assistant/edit/:id

菜单配置:

{
  path: '/assistant/edit/:id',
  title: '编辑智能体',
}

结果:面包屑显示 "首页 / 岗位助理配置 / 编辑智能体"

场景 2:带参数显示

路由:/assistant/edit/123

路由配置:

{
  path: '/assistant/edit/:id',
  meta: {
    title: '编辑',
    breadcrumbTitleFormat: '{title} #{param}',
  },
}

结果:面包屑显示 "首页 / 岗位助理配置 / 编辑 #123"

场景 3:动态名称显示

路由:/assistant/edit/123

路由配置:

{
  path: '/assistant/edit/:id',
  meta: {
    title: '编辑',
    breadcrumbTitleGenerator: (menuTitle, routeParams) => {
      const agent = getAgentFromStore(routeParams.id);
      return agent ? `编辑 ${agent.name}` : menuTitle;
    },
  },
}

结果:面包屑显示 "首页 / 岗位助理配置 / 编辑 智能体A"

注意事项

  1. 路径格式:菜单配置中的动态路由路径必须使用 :param 格式(如 :id)
  2. 路径匹配:系统会自动处理路径匹配,无需手动转换
  3. 面包屑路径:动态路由的面包屑路径会使用实际路径,而不是菜单配置中的路径(包含 :id)
  4. 性能考虑:自定义标题生成函数如果是异步的,建议使用缓存避免重复请求

相关文件

  • src/config/menus.ts - 菜单工具函数(isPathMatch, getMenuChain)
  • src/layouts/BasicLayout.vue - 面包屑生成逻辑
  • src/layouts/components/Breadcrumb.vue - 面包屑组件