//引入views文件夹下所有vue文件
//vite用法
//const modules = import.meta.glob('@/views/**/*.vue')
//webpack用法
//const modules = require.context('@/views', true, /\.vue$/);
//菜单数据结构
let menu = [
{
name: '系统管理',
value: 'systemManage',
icon: 'sfont system-menu',
component: 'layout',
id: '001',
children: [
{
name: '角色管理',
value: 'role',
component: 'layout',
parentId: '001',
id: '001001',
children: [
{
name: '菜单管理',
value: 'menu',
component: 'systemManage/role/menu',
parentId: '001001',
id: '001001001'
},
{
name: '权限管理',
value: 'authority',
component: 'systemManage/role/authority',
parentId: '001001',
id: '001001002'
}
]
},
{
name: '用户管理',
value: 'user',
component: 'systemManage/user',
parentId: '001',
id: '001002'
}
]
},
{
name: '设备管理',
value: 'equipmentManage',
icon: 'sfont system-24gl-printer',
component: 'layout',
id: '002',
children: [
{
name: '设备清单',
value: 'equipment',
component: 'equipmentManage/equipment',
parentId: '002',
id: '002001'
}
]
}
]
//处理过程
const handleMenu = (list) => {
list.map(item => {
let obj = {
title: '',
icon: ''
}
obj.title = item.name
if (item.children) {
if (item.parentId) {
item.component = ''
item.path = item.value
obj.icon = ''
} else {
item.component = 'layout'
item.path = '/' + item.value
obj.icon = item.icon
}
item.children = handleMenu(item.children)
} else {
item.component = item.component
item.path = item.value
}
// item.component = modules['/src/views/' + item.component + '/index.vue']
//变为component: () => import('/src/views/systemManage/user/index.vue')格式
item.meta = obj
delete item.id
delete item.name
delete item.value
delete item.parentId
return item
})
return list
}
const newMenu = handleMenu(menu)
// console.log(newMenu)
//处理成最终数据
let router = [
{
path: '/systemManage',
component: 'layout',
meta: {
title: '系统管理',
icon: 'sfont system-menu'
},
children: [
{
path: 'role',
meta: {
title: '角色管理'
},
redirect: '',
children: [
{
path: 'menu',
component: 'systemManage/role/menu',
meta: {
title: '菜单管理'
}
},
{
path: 'authority',
component: 'systemManage/role/authority',
meta: {
title: '权限管理'
}
}
]
},
{
path: 'user',
component: 'systemManage/user/index.vue',
meta: {
title: '用户管理'
}
}
]
},
{
path: '/equipmentManage',
component: 'layout',
meta: {
title: '设备管理',
icon: 'sfont system-24gl-printer',
},
children: [
{
path: 'equipment',
component: 'equipmentManage/equipment/index.vue',
meta: {
title: '设备清单'
}
}
]
}
]
console