https://github.com/PanJiaChen/vue-admin-template.git
添加组件path==null时, 路由
\src\store\modules\permission.js
import { constantRoutes } from '@/router'
function formatRoutes(routes) {
const fmRoutes = []
routes.forEach(router => {
var routerStr = JSON.stringify(router)
if (router.component) {
let component = router.component
if (component == 'Layout') {
router.component = resolve => {
require(['@/layout/index.vue'], resolve)
}
if (router.children.length > 0) {
var hadIndex = false
var redirect = ''
for (var i = 0; i < router.children.length; i++) {
if (router.children && router.children[i].path) {
if (router.children[i].component.indexOf('/index') != -1) {
hadIndex = true
router.redirect = router.children[i].path
break
}else if (!redirect){
redirect = router.children[i].path
}
}
}
if (!hadIndex && !!redirect) {
router.redirect = redirect
}
if (!redirect && !router.redirect || !router.path) {
console.log('route = ' + routerStr)
console.log('error route :' + JSON.stringify(router))
return
}
}
// if(router.children && router.children[0]){
// router.redirect = router.children[0].path
// }
} else {
delete router.redirect
router.component = resolve => {
require(['@/views/' + component + '.vue'], resolve)
}
}
}
let children = router.children
if (children && children instanceof Array) {
let children = router.children.filter(item => item.component)
if (children.length > 0) {
children = formatRoutes(children)
router.children = children
} else {
delete router.children
}
}
if (!router.path) {
console.log('illegal router : ' + JSON.stringify(router))
return
}
fmRoutes.push(router)
})
return fmRoutes
}
const state = {
menuIndex: parseInt(localStorage.getItem('menuIndex')) || 0,
menus: [],
routes: [],
addRoutes: [],
appKey: '',
homeUrl: ''
}
const mutations = {
SET_ROUTES: (state, routes) => {
state.addRoutes = routes
state.routes = constantRoutes.concat(routes)
},
SET_MENUS: (state, menus) => {
state.menus = menus
},
SET_MENUINDEX: (state, index) => {
state.menuIndex = index
},
SET_APPKEY: (state, key) => {
state.appKey = key
},
SET_HOMEURL: (state, url) => {
state.homeUrl = url
},
SET_WSCONTEXT: (state, data) => {
state.WSContext = data
}
}
const actions = {
generateRoutes({ commit, state }, { menus, path }) {
if (path) {
localStorage.path = path
const index = menus.findIndex(item => item.path == path)
commit('SET_MENUINDEX', index)
}
let index
if (state.menuIndex + 1 > menus.length) {
commit('SET_MENUINDEX', index = 0)
} else {
let str = window.location.href
// if(str.indexOf("workBench?menuIndex=3") != -1){
// index=3
// commit('SET_MENUINDEX', 3)
// } else
if (str.indexOf('workBench?menuPath=work') != -1) {
index = menus.findIndex(item => item.path == 'work')
commit('SET_MENUINDEX', index)
} else {
index = state.menuIndex
}
}
commit('SET_MENUS', menus)
commit('SET_APPKEY', menus[index].path)
const homeUrl = menus[index].homeUrl
commit('SET_HOMEURL', homeUrl ? homeUrl : '')
console.log('menus = ' + JSON.stringify(menus[index].children))
return new Promise(resolve => {
let accessedRoutes = formatRoutes(menus[index].children)
accessedRoutes.push({ path: '*', redirect: '/', hidden: true })
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}