refactor(router): 重构路由菜单生成逻辑并新增模块化路由

移除本地菜单列表的依赖,改为完全从后端API获取菜单数据
新增 profile 和 dashboard 模块化路由文件
简化菜单生成逻辑,避免不必要的深拷贝操作
This commit is contained in:
dap
2026-02-10 11:01:46 +08:00
parent b788a7d860
commit ed42d9de65
3 changed files with 35 additions and 44 deletions

View File

@@ -10,14 +10,10 @@ import type { Menu } from '#/api';
import { generateAccessible } from '@vben/access';
import { preferences } from '@vben/preferences';
import { cloneDeep } from 'lodash-es';
import { getAllMenusApi } from '#/api';
import { BasicLayout, IFrameView } from '#/layouts';
import { $t } from '#/locales';
import { localMenuList } from './routes/local';
const forbiddenComponent = () => import('#/views/_core/fallback/forbidden.vue');
const NotFoundComponent = () => import('#/views/_core/fallback/not-found.vue');
@@ -249,10 +245,7 @@ async function generateAccess(options: GenerateMenuAndRoutesOptions) {
const backMenuList = await getAllMenusApi();
// 转换为vben能用的路由
const vbenMenuList = backMenuToVbenMenu(backMenuList);
// 特别注意 这里要深拷贝
const menuList = [...cloneDeep(localMenuList), ...vbenMenuList];
// console.log('menuList', menuList);
return menuList;
return vbenMenuList;
},
// 可以指定没有权限跳转403页面
forbiddenComponent,

View File

@@ -1,41 +1,19 @@
import type { RouteRecordStringComponent } from '@vben/types';
import type { RouteRecordRaw } from 'vue-router';
import { $t } from '@vben/locales';
import { IFrameView } from '@vben/layouts';
import { $t } from '#/locales';
const {
version,
// vite inject-metadata 插件注入的全局变量
} = __VBEN_ADMIN_METADATA__ || {};
/**
*
*
*/
const localRoutes: RouteRecordStringComponent[] = [
const routes: RouteRecordRaw[] = [
{
component: '/_core/profile/index',
meta: {
icon: 'mingcute:profile-line',
title: $t('ui.widgets.profile'),
hideInMenu: true,
requireHomeRedirect: true,
},
name: 'Profile',
path: '/profile',
},
];
/**
*
*/
export const localMenuList: RouteRecordStringComponent[] = [
{
component: 'BasicLayout',
meta: {
order: -1,
title: 'page.dashboard.title',
// 不使用基础布局(仅在顶级生效)
noBasicLayout: true,
title: $t('page.dashboard.title'),
},
name: 'Dashboard',
path: '/',
@@ -44,24 +22,24 @@ export const localMenuList: RouteRecordStringComponent[] = [
{
name: 'Analytics',
path: '/analytics',
component: '/dashboard/analytics/index',
component: () => import('#/views/dashboard/analytics/index.vue'),
meta: {
affixTab: true,
title: 'page.dashboard.analytics',
title: $t('page.dashboard.analytics'),
},
},
{
name: 'Workspace',
path: '/workspace',
component: '/dashboard/workspace/index',
component: () => import('#/views/dashboard/workspace/index.vue'),
meta: {
title: 'page.dashboard.workspace',
title: $t('page.dashboard.workspace'),
},
},
{
name: 'VbenDocument',
path: '/vben-admin/document',
component: 'IFrameView',
component: IFrameView,
meta: {
icon: 'lucide:book-open-text',
iframeSrc: 'https://dapdap.top',
@@ -72,7 +50,7 @@ export const localMenuList: RouteRecordStringComponent[] = [
{
name: 'V5UpdateLog',
path: '/changelog',
component: '/演示使用自行删除/changelog/index',
component: () => import('#/views/演示使用自行删除/changelog/index.vue'),
meta: {
icon: 'lucide:book-open-text',
keepAlive: true,
@@ -84,7 +62,7 @@ export const localMenuList: RouteRecordStringComponent[] = [
],
},
{
component: '/_core/about/index',
component: () => import('#/views/_core/about/index.vue'),
meta: {
icon: 'lucide:copyright',
order: 9999,
@@ -93,5 +71,6 @@ export const localMenuList: RouteRecordStringComponent[] = [
name: 'About',
path: '/vben-admin/about',
},
...localRoutes,
];
export default routes;

View File

@@ -0,0 +1,19 @@
import type { RouteRecordRaw } from 'vue-router';
import { $t } from '@vben/locales';
const routes: RouteRecordRaw[] = [
{
meta: {
order: -1,
title: $t('ui.widgets.profile'),
hideInMenu: true,
requireHomeRedirect: true,
},
name: 'Profile',
path: '/profile',
component: () => import('#/views/_core/profile/index.vue'),
},
];
export default routes;