修改:解析动态路由
This commit is contained in:
parent
cbde48ba76
commit
a5335b9e9c
@ -61,11 +61,12 @@ router.beforeEach(async(to, from, next) => {
|
||||
|
||||
function setMenuList(next, to){
|
||||
if(store.getters.menuList.length === 0){
|
||||
store.dispatch('user/setMenuList').then(() => {
|
||||
if(window.location.pathname === '/login'){
|
||||
next({path: store.getters.menuList[0].path})
|
||||
store.dispatch('user/setMenuList').then((res) => {
|
||||
const to = window.location.pathname
|
||||
if(findPath(res.data, to)){
|
||||
next({path: to})
|
||||
} else {
|
||||
next({path: window.location.pathname})
|
||||
next({path: store.getters.menuList[0].path})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
@ -73,6 +74,27 @@ function setMenuList(next, to){
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* 查找菜单里有没有该路径
|
||||
* @param menuList
|
||||
* @param to 该路径(当前路径)
|
||||
* @param parentPath
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function findPath(menuList, to, parentPath){
|
||||
for( let i = 0; i < menuList.length - 1; i++) {
|
||||
const menu = menuList[i]
|
||||
if(menu.children && menu.children.length > 0){
|
||||
return findPath(menu.children, to, menu.path)
|
||||
}
|
||||
if(parentPath){
|
||||
menu.path = parentPath + '/' + menu.path
|
||||
}
|
||||
if(menu.path === to) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
router.afterEach(() => {
|
||||
// 完成进度条
|
||||
NProgress.done()
|
||||
|
||||
@ -341,12 +341,13 @@ const router = createRouter()
|
||||
|
||||
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
|
||||
export function resetRouter(params) {
|
||||
router.options.routes = []
|
||||
const newRouter = createRouter()
|
||||
// router.matcher = newRouter.matcher // reset router
|
||||
if(params){
|
||||
newRouter.addRoutes(params)
|
||||
}
|
||||
newRouter.addRoutes(dynamicRouters)
|
||||
router.matcher = newRouter.matcher // reset router
|
||||
}
|
||||
router.addRoutes(dynamicRouters)
|
||||
export default router
|
||||
|
||||
@ -2,7 +2,6 @@ import { login, logout, getInfo } from '@/api/user'
|
||||
import menuApi from '@/api/menu'
|
||||
import { getToken, setToken, getConsumerId, setConsumerId, removeToken, removeConsumerId , setRememberName, removeRememberName } from '@/utils/auth'
|
||||
import { resetRouter } from '@/router'
|
||||
import getters from "@/store/getters"
|
||||
import router from '@/router'
|
||||
import Layout from '@/layout'
|
||||
import ParentView from '@/components/ParentView'
|
||||
@ -47,12 +46,11 @@ const mutations = {
|
||||
const actions = {
|
||||
|
||||
async setMenuList({ commit, state }) {
|
||||
let noeFound = [];
|
||||
return new Promise( (resolve,reject) => {
|
||||
menuApi.menuTree({userId: getters.userId}).then((res) => {
|
||||
menuApi.menuTree({userId: state.userId}).then((res) => {
|
||||
state.menuList = getMenuTree(res.data);
|
||||
commit('SET_MENU_LIST',state.menuList);
|
||||
router.addRoutes(state.menuList)
|
||||
resetRouter(state.menuList)
|
||||
router.options.routes = router.options.routes.concat(state.menuList);
|
||||
resolve(res)
|
||||
}).catch(error => {
|
||||
|
||||
@ -241,6 +241,8 @@ export default {
|
||||
data.append(formKey,this.form[formKey])
|
||||
}
|
||||
}
|
||||
data.delete("createTime")
|
||||
data.delete("updateTime")
|
||||
return data
|
||||
},
|
||||
// 保存
|
||||
|
||||
@ -207,6 +207,7 @@ export default {
|
||||
}
|
||||
}
|
||||
data.delete("createTime")
|
||||
data.delete("updateTime")
|
||||
return data
|
||||
},
|
||||
// 保存
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user