优化生成路由相关代码

This commit is contained in:
hooray 2021-11-11 09:27:55 +08:00
parent 95a192b138
commit b36a765fdb
2 changed files with 6 additions and 13 deletions

View File

@ -150,14 +150,9 @@ router.beforeEach(async(to, from, next) => {
} else {
let accessRoutes = []
if (!store.state.settings.enableBackendReturnRoute) {
accessRoutes = await store.dispatch('menu/generateRoutesAtFront', {
asyncRoutes,
currentPath: to.path
})
accessRoutes = await store.dispatch('menu/generateRoutesAtFront', asyncRoutes)
} else {
accessRoutes = await store.dispatch('menu/generateRoutesAtBack', {
currentPath: to.path
})
accessRoutes = await store.dispatch('menu/generateRoutesAtBack')
}
accessRoutes.push(lastRoute)
let removeRoutes = []

View File

@ -169,19 +169,18 @@ const getters = {
const actions = {
// 根据权限动态生成路由(前端生成)
generateRoutesAtFront({ rootState, dispatch, commit }, data) {
generateRoutesAtFront({ rootState, dispatch, commit }, asyncRoutes) {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async resolve => {
let accessedRoutes
// 如果权限功能开启,则需要对路由数据进行筛选过滤
if (rootState.settings.enablePermission) {
const permissions = await dispatch('user/getPermissions', null, { root: true })
accessedRoutes = filterAsyncRoutes(data.asyncRoutes, permissions)
accessedRoutes = filterAsyncRoutes(asyncRoutes, permissions)
} else {
accessedRoutes = deepClone(data.asyncRoutes)
accessedRoutes = deepClone(asyncRoutes)
}
commit('setRoutes', accessedRoutes)
commit('setHeaderActived', data.currentPath)
let routes = []
accessedRoutes.map(item => {
routes.push(...item.children)
@ -200,7 +199,7 @@ const actions = {
})
},
// 生成路由(后端获取)
generateRoutesAtBack({ rootState, dispatch, commit }, data) {
generateRoutesAtBack({ rootState, dispatch, commit }) {
return new Promise(resolve => {
api.get('route/list', {
baseURL: '/mock/'
@ -215,7 +214,6 @@ const actions = {
accessedRoutes = deepClone(asyncRoutes)
}
commit('setRoutes', accessedRoutes)
commit('setHeaderActived', data.currentPath)
let routes = []
accessedRoutes.map(item => {
routes.push(...item.children)