gin-vue-admin 2.0代码重构
This commit is contained in:
58
web/src/store/module/router.js
Normal file
58
web/src/store/module/router.js
Normal file
@@ -0,0 +1,58 @@
|
||||
import { asyncRouterHandle } from '@/utils/asyncRouter';
|
||||
|
||||
import { asyncMenu } from '@/api/menu'
|
||||
|
||||
|
||||
const formatRouter = (routes) => {
|
||||
routes && routes.map(item => {
|
||||
item.meta.hidden = item.hidden
|
||||
if (item.children.length > 0) {
|
||||
formatRouter(item.children)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const router = {
|
||||
namespaced: true,
|
||||
state: {
|
||||
asyncRouters: []
|
||||
},
|
||||
mutations: {
|
||||
// 设置动态路由
|
||||
setAsyncRouter(state, asyncRouters) {
|
||||
state.asyncRouters = asyncRouters
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
// 从后台获取动态路由
|
||||
async SetAsyncRouter({ commit }) {
|
||||
const baseRouter = [{
|
||||
path: '/layout',
|
||||
name: 'layout',
|
||||
component: "view/layout/index.vue",
|
||||
meta: {
|
||||
title: "底层layout"
|
||||
},
|
||||
children: []
|
||||
}]
|
||||
const asyncRouterRes = await asyncMenu()
|
||||
const asyncRouter = asyncRouterRes.data.menus
|
||||
formatRouter(asyncRouter)
|
||||
baseRouter[0].children = asyncRouter
|
||||
baseRouter.push({
|
||||
path: '*',
|
||||
redirect: '/404'
|
||||
|
||||
})
|
||||
asyncRouterHandle(baseRouter)
|
||||
commit('setAsyncRouter', baseRouter)
|
||||
return true
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
// 获取动态路由
|
||||
asyncRouters(state) {
|
||||
return state.asyncRouters
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user