permission.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import router, { constantRoutes } from '@/router'
  2. /**
  3. * 变量存储
  4. */
  5. export const state = {
  6. routes: [], //最后改变完呈现的路由
  7. role: 'admin', //权限admin 目前先写死了
  8. }
  9. /**
  10. * 获取数据
  11. */
  12. export const getters = {
  13. getterPermission(state) {
  14. return state
  15. }
  16. }
  17. /**
  18. * 改变数据事件
  19. */
  20. export const mutations = {
  21. addRouter(state) {
  22. router.options.routes = state.routes //手动改变路由数组
  23. },
  24. setRouter(state, asyncRouter) {
  25. router.addRoutes(asyncRouter) //注册路由
  26. state.routes = constantRoutes.concat(asyncRouter) //拼接原来的和异步的路由得到最新路由
  27. },
  28. setRole(state, role) {
  29. state.role = role
  30. }
  31. }
  32. /**
  33. * 使用改变数据事件
  34. */
  35. export const actions = {
  36. /** 异步添加路由
  37. * @param {Array} asyncRouter 异步路由
  38. */
  39. async asyncAddRouters({ commit }, asyncRouter = []) {
  40. await commit('setRouter', asyncRouter)
  41. await commit('addRouter')
  42. },
  43. /** 设置角色
  44. * @param {String} role 角色名
  45. */
  46. setPermissionRole({ commit }, role) {
  47. commit('setRole', role)
  48. }
  49. }