Skip to content

路由、菜单与多语言

业务插件通常需要同时声明三部分:

  • routes 决定哪些页面可以访问。
  • menus 决定侧边栏显示哪些入口。
  • locales 为页面标题和菜单提供文案。

三者通过路径和国际化 key 协作,但彼此不会自动生成。

插件路由

ts
import type { AdminPlugin, AdminRouteRecordRaw } from 'vue-bag-admin'

const routes: AdminRouteRecordRaw[] = [
  {
    path: '/reports',
    name: 'ReportList',
    component: () => import('../views/ReportList.vue'),
    meta: {
      title: 'report.list',
      layout: 'default',
      permissions: ['report.read']
    }
  },
  {
    path: '/reports/:id',
    name: 'ReportDetail',
    component: () => import('../views/ReportDetail.vue'),
    meta: {
      title: 'report.detail',
      layout: 'default',
      permissions: ['report.read'],
      activeMenu: '/reports',
      noCache: true
    }
  }
]

const reportPlugin: AdminPlugin = {
  id: 'report-plugin',
  name: '报表插件',
  version: '1.0.0',
  routes
}

插件启用后,bootstrapPlugins() 会调用 router.addRoute() 注册这些路由。

路由 Meta

字段说明
title页面标题或 i18n key
layout布局名,常用 defaultblank
public是否允许未登录访问
roles允许访问的角色
roleMode角色匹配方式:anyall
permissions允许访问的权限点
permissionMode权限匹配方式:anyall
policy自定义权限函数
activeMenu当前页面高亮的菜单路径
noCache是否跳过页面缓存
cacheKey复用组件时的统一缓存键
hidden路由层隐藏标记;侧边栏是否显示仍由 menus 决定

详情和编辑页一般不写入菜单,而是设置:

ts
meta: {
  activeMenu: '/reports', // 返回列表所属菜单
  noCache: true // 离开页面后不保留编辑状态
}

嵌套路由

ts
{
  path: '/settings',
  name: 'Settings',
  component: () => import('../views/Settings.vue'),
  redirect: '/settings/base',
  children: [
    {
      path: 'base',
      name: 'SettingsBase',
      component: () => import('../views/SettingsBase.vue')
    },
    {
      path: 'security',
      name: 'SettingsSecurity',
      component: () => import('../views/SettingsSecurity.vue')
    }
  ]
}

子路由使用相对路径时,最终地址分别是 /settings/base/settings/security

插件菜单

ts
const reportPlugin: AdminPlugin = {
  id: 'report-plugin',
  name: '报表插件',
  version: '1.0.0',
  menus: [
    {
      path: '/report-group',
      title: 'report.title',
      icon: 'star',
      sort: 50,
      permissions: ['report.read'],
      children: [
        {
          path: '/reports',
          title: 'report.list',
          sort: 1,
          permissions: ['report.read']
        }
      ]
    }
  ]
}

菜单字段:

字段说明
path菜单唯一路径或分组标识
title菜单标题或 i18n key
icon宿主支持的图标名
sort同级排序
hidden是否隐藏菜单项
badge菜单徽标
roles可见角色
permissions可见权限点
children子菜单

菜单的角色数组和权限数组默认采用 any。同时配置角色和权限时,两组条件都要通过。

父级 path 可以只是分组标识,叶子菜单的 path 通常应与实际路由一致。

多语言

插件当前支持 zh-CNen

ts
locales: {
  'zh-CN': {
    report: {
      title: '报表管理',
      list: '报表列表',
      detail: '报表详情'
    }
  },
  en: {
    report: {
      title: 'Reports',
      list: 'Report list',
      detail: 'Report detail'
    }
  }
}

路由和菜单引用同一个 key:

ts
meta: {
  title: 'report.list'
}

// 菜单
{
  path: '/reports',
  title: 'report.list'
}

bootstrapPlugins({ i18n }) 会通过 mergeLocaleMessage() 合并插件语言包。

如果未向 bootstrapPlugins() 传入 i18n,插件路由仍会注册,但语言包不会合并。

冲突规则

引导阶段会检查:

  • 插件 id 是否重复。
  • 路由 path 是否与宿主或其他插件重复。
  • 路由 name 是否重复。
  • 菜单 path 是否重复。

推荐给每个插件建立独立命名空间:

text
路由路径   /reports/*
路由名称   Report*
i18n key   report.*
菜单分组   /report-group

下一步

继续阅读权限与配置项

Released under the MIT License.