修改:解析动态路由

This commit is contained in:
jmal 2021-01-11 20:50:08 +08:00
parent c679a04db3
commit cbde48ba76
2 changed files with 26 additions and 47 deletions

View File

@ -1,6 +1,5 @@
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // 进度条
import 'nprogress/nprogress.css' // 进度条样式
import { getToken } from '@/utils/auth' // 认证获取token
@ -61,11 +60,13 @@ router.beforeEach(async(to, from, next) => {
})
function setMenuList(next, to){
console.log('menuList', to, store.getters.menuList)
if(store.getters.menuList.length == 0){
store.dispatch('user/setMenuList').then(res => {
console.log('res', res)
next({path:store.getters.menuList[0].path})
if(store.getters.menuList.length === 0){
store.dispatch('user/setMenuList').then(() => {
if(window.location.pathname === '/login'){
next({path: store.getters.menuList[0].path})
} else {
next({path: window.location.pathname})
}
})
} else {
next(to)

View File

@ -49,17 +49,11 @@ const actions = {
async setMenuList({ commit, state }) {
let noeFound = [];
return new Promise( (resolve,reject) => {
noeFound = []
menuApi.menuTree({userId: getters.userId}).then((res) => {
let data = res.data;
let mentTree = getMenuTree(data, []);
console.log('mentTree', mentTree);
state.menuList = mentTree.concat(noeFound);
state.menuList = getMenuTree(res.data);
commit('SET_MENU_LIST',state.menuList);
router.addRoutes(state.menuList)
console.log('state.menuList', state.menuList)
router.options.routes = router.options.routes.concat(state.menuList);
console.log('router',router);
resolve(res)
}).catch(error => {
reject(error)
@ -141,59 +135,43 @@ const actions = {
}
}
function getMenuTree(menuList, arr) {
console.log('mentTreeData', menuList)
arr = menuList.map((menu, index) => {
function getMenuTree(menuList) {
return menuList.map(menu => {
let router = {}
if(menu.children && menu.children.length){
router.meta = {title: menu.name, icon: menu.icon}
router.children = findChildren(menu.children)
}
if(menu.component && menu.component.length > 0) {
router.children = [
{
path: '',
name: menu.name,
component: loadView(menu.component),
meta: {title: menu.name, icon: menu.icon}
}
]
{
path: '',
name: menu.name,
component: loadView(menu.component),
meta: {title: menu.name, icon: menu.icon}
}
]
}
router.path = menu.path
router.component = Layout
return router
});
return arr;
})
}
function findChildren(childrenMenu){
let children = []
childrenMenu.forEach((menu, index) => {
if(menu.children && menu.children.length){
if(!menu.component || menu.component.length === 0){
menu.children.forEach(c => {
let router = {}
router.path = c.path
router.name = c.name
router.component = loadView(c.component)
router.meta = {
title: c.name,
icon: c.icon
}
if(c.children && c.children.length){
children = children.concat(findChildren(c.children, c))
return
}
children.push(router)
})
return
}
}
childrenMenu.forEach(menu => {
let router = {
path: menu.path,
name: menu.name,
component: loadView(menu.component),
component: !menu.component || menu.component.length === 0 ? ParentView : loadView(menu.component),
meta: {title: menu.name, icon: menu.icon}
}
if(menu.children && menu.children.length){
if(!menu.component || menu.component.length === 0){
router.children = findChildren(menu.children)
}
}
children = children.concat(router)
})
return children