53 lines
1.4 KiB
JavaScript
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
|
|
}
|