spa/src/router/generateIndexRouter.js
2025-03-29 11:46:12 +08:00

53 lines
1.4 KiB
JavaScript

export function generateIndexRouter(data) {
let indexRouter = [
{
path: '/',
name: 'root',
component: () =>
import(/* webpackChunkName: "Layout" */ '@/components/layouts/GlobalLayout.vue'),
redirect: '/home',
children: generateChildRouters(data),
},
{
path: '*',
redirect: '/404',
hidden: true,
},
]
return indexRouter
}
// 生成嵌套路由(子路由)
function generateChildRouters(data) {
const routers = []
for (const item of data) {
let componentPath = ''
if (item.component.indexOf('layouts') >= 0) {
componentPath = 'components/' + item.component
} else {
componentPath = 'views/' + item.component
}
let menu = {
path: item.path,
name: item.name,
// component: resolve => require(['@/' + componentPath + '.vue'], resolve),
component: () => import(`@/${componentPath}.vue`),
redirect: item.redirect || null,
meta: { ...item.meta },
}
if (item.alwaysShow) {
menu.component = () => import('@/components/layouts/SideTabsLayout.vue')
menu.redirect = null
}
if (item.meta.url) {
menu.component = null
}
if (item.children?.length > 0 && !item.alwaysShow) {
// if (item.children?.length > 0) {
menu.children = [...generateChildRouters(item.children)]
}
routers.push(menu)
}
return routers
}