mirror of
https://github.com/iczer/vue-antd-admin
synced 2025-04-05 07:27:06 +08:00
feat: add deep merge routes function for routerUtil.js; ⭐
新增:routerUtil.js 工具新增深度合并路由方法;
This commit is contained in:
parent
4db7c2deff
commit
8102c8a924
@ -1,6 +1,7 @@
|
||||
import routerMap from '@/router/async/router.map'
|
||||
import {mergeI18nFromRoutes} from '@/utils/i18n'
|
||||
import Router from 'vue-router'
|
||||
import deepMerge from 'deepmerge'
|
||||
|
||||
/**
|
||||
* 根据 路由配置 和 路由组件注册 解析路由
|
||||
@ -95,6 +96,44 @@ function mergeRoutes(target, source) {
|
||||
return Object.values(routesMap)
|
||||
}
|
||||
|
||||
/**
|
||||
* 深度合并路由
|
||||
* @param target {Route[]}
|
||||
* @param source {Route[]}
|
||||
* @returns {Route[]}
|
||||
*/
|
||||
function deepMergeRoutes(target, source) {
|
||||
// 映射路由数组
|
||||
const mapRoutes = routes => {
|
||||
const routesMap = {}
|
||||
routes.forEach(item => {
|
||||
routesMap[item.path] = {
|
||||
...item,
|
||||
children: item.children ? mapRoutes(item.children) : undefined
|
||||
}
|
||||
})
|
||||
return routesMap
|
||||
}
|
||||
const tarMap = mapRoutes(target)
|
||||
const srcMap = mapRoutes(source)
|
||||
|
||||
// 合并路由
|
||||
const merge = deepMerge(tarMap, srcMap)
|
||||
|
||||
// 转换为 routes 数组
|
||||
const parseRoutesMap = routesMap => {
|
||||
return Object.values(routesMap).map(item => {
|
||||
if (item.children) {
|
||||
item.children = parseRoutesMap(item.children)
|
||||
} else {
|
||||
delete item.children
|
||||
}
|
||||
return item
|
||||
})
|
||||
}
|
||||
return parseRoutesMap(merge)
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化路由的权限配置
|
||||
* @param routes 路由
|
||||
@ -164,4 +203,4 @@ function loadGuards(guards, options) {
|
||||
})
|
||||
}
|
||||
|
||||
export {parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards}
|
||||
export {parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards, deepMergeRoutes}
|
||||
|
Loading…
x
Reference in New Issue
Block a user