前置要点 每页都要有name
/**
* 生成扁平化机构路由(仅两级结构)
* @param {允许访问的路由Tree} accessRoutes
* 路由基本机构:
* {
* name: String,
* path: String,
* component: Component,
* redirect: String,
* children: [
* ]
* }
*/
function generateFlatRoutes(accessRoutes) {
const flatRoutes = []
for (const item of accessRoutes) {
let childrenFflatRoutes = []
if (item.children && item.children.length > 0) {
childrenFflatRoutes = castToFlatRoute(item.children, '')
}
// 一级路由是布局路由,需要处理的只是其子路由数据
flatRoutes.push({
name: item.name,
path: item.path,
component: item.component,
redirect: item.redirect,
children: childrenFflatRoutes
})
}
return flatRoutes
}
/**
* 将子路由转换为扁平化路由数组(仅一级)
* @param {待转换的子路由数组} routes
* @param {父级路由路径} parentPath
*/
function castToFlatRoute(routes, parentPath, flatRoutes = []) {
for (const item of routes) {
if (item.children && item.children.length > 0) {
if (item.redirect && item.redirect !== 'noRedirect') {
flatRoutes.push({
name: item.name,
path: (parentPath + '/' + item.path).substring(1),
redirect: item.redirect,
meta: item.meta
})
}
castToFlatRoute(item.children, parentPath + '/' + item.path, flatRoutes)
} else {
flatRoutes.push({
name: item.name,
path: (parentPath + '/' + item.path).substring(1),
component: item.component,
meta: item.meta
})
}
}
return flatRoutes
}
generateRoutes({ commit }, roles) {
return new Promise(resolve => {
// let accessedRoutes
// if (roles.includes('admin')) {
// accessedRoutes = asyncRoutes || []
// } else {
// accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
// }
const accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
// 重点修改 下边
const flatRoutes = generateFlatRoutes(accessedRoutes)
commit('SET_ROUTES', accessedRoutes)
resolve(flatRoutes)
})
}
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
const { roles } = await store.dispatch('user/getInfo')
// generate accessible routes map based on roles
// 下边修改接参变量命名 与 返回变量名一致 不至于理解混乱
const flatRoutes = await store.dispatch('permission/generateRoutes', roles)
// dynamically add accessible routes
router.addRoutes(flatRoutes)
/**
* 生成扁平化机构路由(仅两级结构)
* @param {允许访问的路由Tree} accessRoutes
* 路由基本机构:
* {
* name: String,
* path: String,
* component: Component,
* redirect: String,
* children: [
* ]
* }
*/
function generateFlatRoutes(accessRoutes) {
const flatRoutes = []
for (const item of accessRoutes) {
let childrenFflatRoutes = []
if (item.children && item.children.length > 0) {
childrenFflatRoutes = castToFlatRoute(item.children, '')
}
// 一级路由是布局路由,需要处理的只是其子路由数据
flatRoutes.push({
name: item.name,
path: item.path,
component: item.component,
redirect: item.redirect,
children: childrenFflatRoutes
})
}
return flatRoutes
}
/**
* 将子路由转换为扁平化路由数组(仅一级)
* @param {待转换的子路由数组} routes
* @param {父级路由路径} parentPath
*/
function castToFlatRoute(routes, parentPath, flatRoutes = []) {
for (const item of routes) {
if (item.children && item.children.length > 0) {
if (item.redirect && item.redirect !== 'noRedirect') {
flatRoutes.push({
name: item.name,
path: (parentPath + '/' + item.path).substring(1),
redirect: item.redirect,
meta: item.meta
})
}
castToFlatRoute(item.children, parentPath + '/' + item.path, flatRoutes)
} else {
flatRoutes.push({
name: item.name,
path: (parentPath + '/' + item.path).substring(1),
component: item.component,
meta: item.meta
})
}
}
return flatRoutes
}
export const loadMenus = (next, to) => {
buildMenus().then(res => {
const asyncRouter = filterAsyncRouter(res)
const flatRoutes = generateFlatRoutes(asyncRouter)
asyncRouter.push({ path: '*', redirect: '/404', hidden: true })
store.dispatch('GenerateRoutes', asyncRouter).then(() => { // 存储路由
router.addRoutes(flatRoutes) // 动态添加可访问路由表
next({ ...to, replace: true })
})
})
}
以上方法 亲测可用
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有