修改:解析动态路由
This commit is contained in:
parent
c679a04db3
commit
cbde48ba76
@ -1,6 +1,5 @@
|
|||||||
import router from './router'
|
import router from './router'
|
||||||
import store from './store'
|
import store from './store'
|
||||||
import { Message } from 'element-ui'
|
|
||||||
import NProgress from 'nprogress' // 进度条
|
import NProgress from 'nprogress' // 进度条
|
||||||
import 'nprogress/nprogress.css' // 进度条样式
|
import 'nprogress/nprogress.css' // 进度条样式
|
||||||
import { getToken } from '@/utils/auth' // 认证获取token
|
import { getToken } from '@/utils/auth' // 认证获取token
|
||||||
@ -61,11 +60,13 @@ router.beforeEach(async(to, from, next) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
function setMenuList(next, to){
|
function setMenuList(next, to){
|
||||||
console.log('menuList', to, store.getters.menuList)
|
if(store.getters.menuList.length === 0){
|
||||||
if(store.getters.menuList.length == 0){
|
store.dispatch('user/setMenuList').then(() => {
|
||||||
store.dispatch('user/setMenuList').then(res => {
|
if(window.location.pathname === '/login'){
|
||||||
console.log('res', res)
|
next({path: store.getters.menuList[0].path})
|
||||||
next({path:store.getters.menuList[0].path})
|
} else {
|
||||||
|
next({path: window.location.pathname})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
next(to)
|
next(to)
|
||||||
|
|||||||
@ -49,17 +49,11 @@ const actions = {
|
|||||||
async setMenuList({ commit, state }) {
|
async setMenuList({ commit, state }) {
|
||||||
let noeFound = [];
|
let noeFound = [];
|
||||||
return new Promise( (resolve,reject) => {
|
return new Promise( (resolve,reject) => {
|
||||||
noeFound = []
|
|
||||||
menuApi.menuTree({userId: getters.userId}).then((res) => {
|
menuApi.menuTree({userId: getters.userId}).then((res) => {
|
||||||
let data = res.data;
|
state.menuList = getMenuTree(res.data);
|
||||||
let mentTree = getMenuTree(data, []);
|
|
||||||
console.log('mentTree', mentTree);
|
|
||||||
state.menuList = mentTree.concat(noeFound);
|
|
||||||
commit('SET_MENU_LIST',state.menuList);
|
commit('SET_MENU_LIST',state.menuList);
|
||||||
router.addRoutes(state.menuList)
|
router.addRoutes(state.menuList)
|
||||||
console.log('state.menuList', state.menuList)
|
|
||||||
router.options.routes = router.options.routes.concat(state.menuList);
|
router.options.routes = router.options.routes.concat(state.menuList);
|
||||||
console.log('router',router);
|
|
||||||
resolve(res)
|
resolve(res)
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
reject(error)
|
reject(error)
|
||||||
@ -141,59 +135,43 @@ const actions = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMenuTree(menuList, arr) {
|
function getMenuTree(menuList) {
|
||||||
console.log('mentTreeData', menuList)
|
return menuList.map(menu => {
|
||||||
arr = menuList.map((menu, index) => {
|
|
||||||
let router = {}
|
let router = {}
|
||||||
if(menu.children && menu.children.length){
|
if(menu.children && menu.children.length){
|
||||||
|
router.meta = {title: menu.name, icon: menu.icon}
|
||||||
router.children = findChildren(menu.children)
|
router.children = findChildren(menu.children)
|
||||||
}
|
}
|
||||||
if(menu.component && menu.component.length > 0) {
|
if(menu.component && menu.component.length > 0) {
|
||||||
router.children = [
|
router.children = [
|
||||||
{
|
{
|
||||||
path: '',
|
path: '',
|
||||||
name: menu.name,
|
name: menu.name,
|
||||||
component: loadView(menu.component),
|
component: loadView(menu.component),
|
||||||
meta: {title: menu.name, icon: menu.icon}
|
meta: {title: menu.name, icon: menu.icon}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
router.path = menu.path
|
router.path = menu.path
|
||||||
router.component = Layout
|
router.component = Layout
|
||||||
return router
|
return router
|
||||||
});
|
})
|
||||||
return arr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function findChildren(childrenMenu){
|
function findChildren(childrenMenu){
|
||||||
let children = []
|
let children = []
|
||||||
childrenMenu.forEach((menu, index) => {
|
childrenMenu.forEach(menu => {
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let router = {
|
let router = {
|
||||||
path: menu.path,
|
path: menu.path,
|
||||||
name: menu.name,
|
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}
|
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)
|
children = children.concat(router)
|
||||||
})
|
})
|
||||||
return children
|
return children
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user