123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import router, { constantRoutes } from '@/router'
- /**
- * 变量存储
- */
- export const state = {
- routes: [], //最后改变完呈现的路由
- role: 'admin', //权限admin 目前先写死了
- }
- /**
- * 获取数据
- */
- export const getters = {
- getterPermission(state) {
- return state
- }
- }
- /**
- * 改变数据事件
- */
- export const mutations = {
- addRouter(state) {
- router.options.routes = state.routes //手动改变路由数组
- },
- setRouter(state, asyncRouter) {
- router.addRoutes(asyncRouter) //注册路由
- state.routes = constantRoutes.concat(asyncRouter) //拼接原来的和异步的路由得到最新路由
- },
- setRole(state, role) {
- state.role = role
- }
- }
- /**
- * 使用改变数据事件
- */
- export const actions = {
- /** 异步添加路由
- * @param {Array} asyncRouter 异步路由
- */
- async asyncAddRouters({ commit }, asyncRouter = []) {
- await commit('setRouter', asyncRouter)
- await commit('addRouter')
- },
- /** 设置角色
- * @param {String} role 角色名
- */
- setPermissionRole({ commit }, role) {
- commit('setRole', role)
- }
- }
|