一、获取完整路由记录
1.通过router的getRoutes获取完整路由记录。该数组包含了所有层级的路由,子路由也放在了最外层,需要把最外层的路由过滤掉
/**
* 获取子路由配置
* @param {*} routes
* @returns
*/
function getChildrenRoutes(routes) {
const result = []
routes.forEach((item) => {
if (item.children && item.children.length) {
result.push(...item.children)
}
})
return result
}
/**
* 通过查重,处理脱离层级的路由
*/
export function filterRoutes(routes) {
const childrenRoutes = getChildrenRoutes(routes)
routes = routes.filter((item) => {
return !childrenRoutes.find((singleRoute) => {
return singleRoute.path === item.path
})
})
return routes
}