frontend-router
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese前端路由规范
Frontend Routing Specification
路由组织架构
Routing Organization Architecture
路由模块主要位于 目录下:
apps/web-antd/src/router/text
apps/web-antd/src/router/
├── access.ts # 菜单权限、动态路由生成逻辑,与后端交互生成可访问菜单
├── guard.ts # 路由守卫(加载进度、权限校验、NProgress 等)
├── index.ts # Router 实例创建及初始化入口
├── routes/ # 路由配置目录
│ ├── core.ts # 基础/核心路由(登录、404兜底、Root重定向,无权限校验)
│ ├── index.ts # 路由聚合与扁平化输出
│ └── modules/ # 页面模块化的静态/动态辅助路由配置文件
└── tongji.ts # 统计配置The routing module is mainly located in the directory:
apps/web-antd/src/router/text
apps/web-antd/src/router/
├── access.ts # 菜单权限、动态路由生成逻辑,与后端交互生成可访问菜单
├── guard.ts # 路由守卫(加载进度、权限校验、NProgress 等)
├── index.ts # Router 实例创建及初始化入口
├── routes/ # 路由配置目录
│ ├── core.ts # 基础/核心路由(登录、404兜底、Root重定向,无权限校验)
│ ├── index.ts # 路由聚合与扁平化输出
│ └── modules/ # 页面模块化的静态/动态辅助路由配置文件
└── tongji.ts # 统计配置路由定义规范
Routing Definition Specification
1. 自动与动态解析结合
1. Combination of Automatic and Dynamic Parsing
本项目使用 内部的动态路由生成能力:
vue-vben-admin- 通过后端 API(读取菜单树)拉取前端具有权限的菜单项。
getAuthPermissionInfoApi - 根据 提供的
apps/web-antd/src/views/**/*.vue自动映射页面组件。componentKeys - 如果特定页面需要配置前端静态路由,将其置于 下。
routes/modules/
This project uses the built-in dynamic routing generation capability of :
vue-vben-admin- Pull the menu items that the front-end has permission to access through the back-end API (reads the menu tree).
getAuthPermissionInfoApi - Automatically map page components according to the provided by
componentKeys.apps/web-antd/src/views/**/*.vue - If a specific page needs to be configured with front-end static routing, place it under .
routes/modules/
2. 前端静态辅助路由编写 (routes/modules/*.ts
)
routes/modules/*.ts2. Frontend Static Auxiliary Routing Writing (routes/modules/*.ts
)
routes/modules/*.ts在某些场景(如图表大盘、个人中心、隐藏在菜单外但确实需要的前端路由)下,可以在 中编写:
modulests
import type { RouteRecordRaw } from 'vue-router';
import { $t } from '#/locales';
const routes: RouteRecordRaw[] = [
{
path: '/system/notify-message',
component: () => import('#/views/system/notify/my/index.vue'),
name: 'MyNotifyMessage',
meta: {
title: '我的站内信',
icon: 'ant-design:message-filled',
hideInMenu: true,
},
},
];
export default routes;In some scenarios (such as chart dashboards, personal centers, front-end routes that are hidden outside the menu but actually needed), you can write them in :
modulests
import type { RouteRecordRaw } from 'vue-router';
import { $t } from '#/locales';
const routes: RouteRecordRaw[] = [
{
path: '/system/notify-message',
component: () => import('#/views/system/notify/my/index.vue'),
name: 'MyNotifyMessage',
meta: {
title: '我的站内信',
icon: 'ant-design:message-filled',
hideInMenu: true,
},
},
];
export default routes;3. Route Meta 元信息规范
3. Route Meta Meta Information Specification
使用定制的 ,主要字段必须规范填写:
RouteMeta- : 页面标题,尽量使用
title国际化,或是业务可读中文。$t('page.xxx') - : 菜单图标(使用 Iconify 支持的图标集)。
icon - :
hideInMenu,是否在侧边栏/顶部菜单中隐藏该页面。boolean - :
hideInBreadcrumb,是否在面包屑中隐藏。boolean - :
hideInTab,是否不在多标签页中显示。boolean - :
affixTab,是否在多标签页中固定(比如 Dashboard)。boolean - :
ignoreAccess,是否忽略权限控制。boolean
Use the customized , and the main fields must be filled in standardly:
RouteMeta- : Page title, try to use
titleinternationalization, or business-readable Chinese.$t('page.xxx') - : Menu icon (using icon sets supported by Iconify).
icon - :
hideInMenu, whether to hide the page in the sidebar/top menu.boolean - :
hideInBreadcrumb, whether to hide in the breadcrumb.boolean - :
hideInTab, whether not to display in the multi-tab page.boolean - :
affixTab, whether to fix it in the multi-tab page (such as Dashboard).boolean - :
ignoreAccess, whether to ignore permission control.boolean
路由守卫逻辑 (guard.ts
)
guard.tsRouting Guard Logic (guard.ts
)
guard.ts- 进度条:处理,已加载页面不在切换时二次触发长时间 NProgress。
loadedPaths - 登录校验:非白名单的路由强制校验 ,未登录跳转至
accessStore.accessToken。/auth/login - 权限拉取与动态挂载:若识别已登录但 为
isAccessChecked,则触发false,并经过fetchUserInfo()将动态解析后的路由写入generateAccess中。vue-router
- Progress bar: Processed by , the loaded page will not trigger long-time NProgress twice when switching.
loadedPaths - Login verification: Routes not in the whitelist will force verification of , and jump to
accessStore.accessTokenif not logged in./auth/login - Permission pulling and dynamic mounting: If it is recognized that you are logged in but is
isAccessChecked, triggerfalse, and write the dynamically parsed route intofetchUserInfo()throughvue-router.generateAccess
约束提示
Constraint Tips
- 禁止在 中随意添加业务页面,所有的业务功能页面都必须走权限校验,或作为单独模块存入
router/routes/core.ts中。routes/modules/ - 路径命名风格:尽量保持小写,多单词用连字符拼接。例如 。
/system/user-role - 视图关联:不要手动写很深的层级 import 组件,依赖 实现懒加载路由视图组件。
() => import('#/views/...')
- It is forbidden to add business pages arbitrarily in . All business function pages must go through permission verification, or be stored in
router/routes/core.tsas a separate module.routes/modules/ - Path naming style: Try to keep it lowercase, and use hyphens to splicing multiple words. For example .
/system/user-role - View association: Do not manually write import components with very deep levels, rely on to implement lazy loading of routing view components.
() => import('#/views/...')