- 一次性改变路由导航
router.onReady(() => {
// 菜单权限请求
axios.get('/menus').then(data => {
data.forEach(path => {
// routeChildren 所有路由打平成一维数组
let route = routeChildren.find(r => r.path === path)
router.addRoute('layout', route)
})
})
})
- 每次切换路由再判断是否有权限
const loadMenu = []
router.beforeEach((to, from, next) => {
if (loadMenu.includes(to.path)) {
next()
} else {
// routeChildren 所有路由打平成一维数组
let route = routeChildren.find(r => r.path === to.path)
router.addRoute('layout', route)
loadMenu.push(route.path)
next({ ...to })
}
})