pms/web/src/store/module/router.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-09-14 22:30:04 +08:00
import { asyncRouterHandle } from '@/utils/asyncRouter';
import { asyncMenu } from '@/api/menu'
2020-04-01 15:56:39 +08:00
const formatRouter = (routes) => {
routes && routes.map(item => {
item.meta.hidden = item.hidden
if (item.children && item.children.length > 0) {
2020-04-01 15:56:39 +08:00
formatRouter(item.children)
}
})
}
2019-09-14 22:30:04 +08:00
export const router = {
namespaced: true,
state: {
asyncRouters: []
},
mutations: {
// 设置动态路由
setAsyncRouter(state, asyncRouters) {
state.asyncRouters = asyncRouters
}
},
actions: {
// 从后台获取动态路由
async SetAsyncRouter({ commit }) {
2019-09-19 23:49:10 +08:00
const baseRouter = [{
path: '/layout',
name: 'layout',
component: "view/layout/index.vue",
meta: {
title: "底层layout"
},
children: []
}]
2019-09-14 22:30:04 +08:00
const asyncRouterRes = await asyncMenu()
const asyncRouter = asyncRouterRes.data.menus
asyncRouter.push({
path: "404",
name: "404",
hidden: true,
meta: {
title: "迷路了*。*",
},
component: 'view/error/index.vue'
})
2020-04-01 15:56:39 +08:00
formatRouter(asyncRouter)
2019-09-19 23:49:10 +08:00
baseRouter[0].children = asyncRouter
baseRouter.push({
2019-09-14 22:30:04 +08:00
path: '*',
redirect: '/layout/404'
2019-09-14 22:30:04 +08:00
})
2019-09-19 23:49:10 +08:00
asyncRouterHandle(baseRouter)
commit('setAsyncRouter', baseRouter)
return true
2019-09-14 22:30:04 +08:00
}
},
getters: {
// 获取动态路由
asyncRouters(state) {
return state.asyncRouters
}
}
}