当前位置:   article > 正文

vue3做路由权限管理笔记_vue3 动态路由导航菜单权限管理

vue3 动态路由导航菜单权限管理

vue3.0做路由权限

->(1)首先在vuex里面调用用户信息接口

(*代码如下)

`

  1. const store = createStore({
  2. state() {
  3. return {
  4. //用户信息
  5. user:{},
  6. //定义左侧菜单列表数据
  7. menus:[],
  8. //路由名称
  9. ruleNames:[]
  10. }
  11. },
  12. mutations:{
  13. //定义修改左侧菜单数据
  14. SET_MENUS(state , menus) {
  15. state.menus = menus
  16. },
  17. //定义修改左侧路由名称的数据
  18. SET_RULENAMES(state , ruleNames) {
  19. state.ruleNames = ruleNames
  20. }
  21. }
  22. actions:{
  23. //获取当前登录用户信息
  24. getinfo({ commit }) {
  25. return new Promise((resolve,reject) => {
  26. getinfo().then(res => {
  27. commit("SET_USERINFO",res)
  28. //存储用户信息
  29. commit("SET_MENUS",res.menus)
  30. //把左侧菜单数据存入
  31. commit("SET_RULENAMES",res.ruleNames) //把路由名称存入
  32. //请求成功调用
  33. resolve(res)
  34. }).catch(err => reject(err))
  35. })
  36. }
  37. }
  38. })

`

(2) ->找到router文件夹中的index.js文件

(*做法) `

  1. 在router/index.js中留下*首页路由
  2. *登录路由,*404路由,*其余的路由全部移除
  3. 在创建一个动态路由把其余的路由移到里面去

(*具体代码)`

`

  1. import { createRouter , createWebHistory } from 'vue-router'
  2. import Layout from "@/views/Home/index.vue" //项目总组件
  3. import NotFound from "@/views/404.vue" //404组件
  4. import Login from "@/views/Login/index.vue" //登录组件
  5. import Index from "@/Layout/index.vue" //首页组件
  6. import GoodList from "@/views/goods/list.vue" //导入商品列表组件
  7. import CategoryList from "@/views/category/list.vue" //分类管理
  8. <!-- 静态路由 -->
  9. //默认路由所有用户共享(首页路由,登录路由,404路由)
  10. const routes = [
  11. {
  12. path:'/',
  13. name:"Index",
  14. component:Index,
  15. },
  16. {
  17. path:'/login',
  18. name:'Login',
  19. component:Login,
  20. meta:{
  21. title:'登录'
  22. }
  23. },
  24. <!-- 配置404页面-->
  25. {
  26. path:'/:pathMatch(.*)*',
  27. name:'NotFound',
  28. component:NotFound
  29. }
  30. ]
  31. *** <!-- 动态路由 -->
  32. <!--添加动态路由的数组(用于匹配动态菜单添加动态路由的)-->
  33. const asyncRoutes = [
  34. {
  35. path:'/',
  36. name:"/",
  37. component:Index,
  38. meta:{
  39. title:'首页'
  40. }
  41. },
  42. {
  43. path:'/goods/list',
  44. name:"/goods/list",
  45. component:GoodList,
  46. meta:{
  47. title:'商品管理'
  48. }
  49. },
  50. {
  51. path:'/category/list',
  52. name:'/category/list',
  53. component:CategoryList,
  54. meta:{
  55. title:'分类管理'
  56. }
  57. }
  58. ]
  59. export const router = createRouter({
  60. history:createWebHistory(),
  61. routes
  62. })
  63. <!--使用递归算法添加路由权限-->
  64. export function addRoutes (menus) {
  65. //是否有新的路由(解决加载到路由导航守卫里面出现死循环问题)
  66. let hasNewRoutes = false
  67. <!-- 使用递归算法-->
  68. <!-- 添加动态路由的方法: router.addRoute()-->
  69. const findAndAddRoutesByMenus = (arr) => {
  70. arr.forEach(e => {
  71. let item = asyncRoutes.find(o => o.path == e.frontpath)
  72. if(item && !router.hasRoute(item.path)) {
  73. router.addRoute("Index",item)
  74. hasNewRoutes = true
  75. }
  76. <!-- 是否有子路由-->
  77. if(e.child && e.child.length > 0){
  78. findAndAddRoutesByMenus(e.child)
  79. }
  80. })
  81. }
  82. findAndAddRoutesByMenus(menus)
  83. return hasNewRoutes
  84. }

`

(*把main.js的路由导入方式改掉)

改成 import { router } from './router'

(*把permission的路由导入方式改掉) `

改成:import { router , addRoutes } from "./router"

`

->(3)在导航守卫里面开始调用路由的权限管理代码

(*permission代码如下:)

`

  1. import { router , addRoutes } from "./router"
  2. <!--全局前置守卫-->
  3. router.beforeEach(async(to , from , next) => {
  4. let hasNewRoutes = false
  5. if(token && !hasGetInfo) {
  6. //调用用户接口
  7. // 解构menus
  8. let { menus } = await store.dispatch("getinfo")
  9. hasGetInfo = true
  10. //动态添加路由
  11. hasNewRoutes = addRoutes(menus)
  12. }
  13. hasNewRoutes ? next(to.fullPath) : next() //放行
  14. })

`

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/58896
推荐阅读
相关标签
  

闽ICP备14008679号