erp-el-element/utils/asyncRouter.js

85 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-05-07 11:51:58 +08:00
/*
* @Author: ckycjwds 1240973496@qq.com
* @Date: 2022-11-22 17:56:39
* @LastEditors: ckycjwds 1240973496@qq.com
* @LastEditTime: 2023-03-09 11:24:50
* @FilePath: \aiguo_erp_vue\src\utils\asyncRouter.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
// 引入路由文件这种的公共路由
// import { constantRoutes } from '../router';
// Layout 组件是项目中的主页面切换路由时仅切换Layout中的组件
import Layout from '@/layout'
// import View from '@/layout/View.vue';
import Views from '../layout/View.vue'
// const Views = require('@/layout/View.vue')
import { asyncRoutes, constantRoutes } from '@/router'
export function getAsyncRoutes(routes) {
const res = []
// 定义路由中需要的自定名
const keys = ['path', 'name', 'child', 'redirect', 'meta', 'hidden']
if(routes){
// 遍历路由数组去重组可用的路由
routes.forEach(item => {
// console.log(item)
// this.$set(item, 'meta', { })
const newItem = {}
if (item.name != '控制台菜单(默认存在)') {
if (item.component) {
// 判断 item.component 是否等于 'Layout',若是则直接替换成引入的 Layout 组件
if (item.component === 'Layout') {
newItem.component = Layout
newItem.redirect = item.path
} else if (item.component === 'View' && item.level == 2) { // 判断 item.component 是否等于 'View',若是则直接替换成引入的 View 组件,用于二级分类使用
newItem.component = Views
newItem.redirect = item.child && item.child[0].path
} else {
newItem.component = (resolve) => require([`@/views${item.component}`], resolve)
// newItem.component = (resolve) => require([`@/views${item.component}`], resolve)
// newItem.component = () => import(`@/views${item.component}`)
// newItem.component = (resolve) => require([`@/views/Warehousing/Storageentryworkbench`], resolve)
}
}
for (const key in item) {
if (keys.includes(key)) {
newItem[key] = item[key]
}
newItem.meta = { title: item.name }
if (item.no_cache && item.no_cache == '0') {
newItem.meta.noCache = true
} else {
delete newItem.noCache
}
if (item.hidden && item.hidden == '0') {
newItem.hidden = true
} else {
delete newItem.hidden
}
if (item.ico) {
newItem.meta.icon = item.ico
}
newItem.name = item.code
}
// 若遍历的当前路由存在子路由,需要对子路由进行递归遍历
if (newItem.child && newItem.child.length) {
if (newItem.child.every((e) => { return e.level == 1 })) {
newItem.children = getAsyncRoutes(item.child)
} else if (newItem.child.every((e) => { return e.level == 2 })) {
newItem.children = getAsyncRoutes(item.child)
} else if (newItem.child.every((e) => { return e.level == 3 }) && newItem.child.every((e) => { return e.group != 2 })) { // newItem.child.every((e)=>{return e.group!=2}) 防止二级下只有按钮
newItem.children = getAsyncRoutes(item.child)
}
}
delete newItem.child;
newItem.meta.keepAlive = true
res.push(newItem);
}
})
}
// console.log(res)
return res
}