vue 菜单权限

本文介绍了在Vue项目中如何实现动态菜单权限控制,通过分析`src/router/index.js`、`src/store/getters.js`、`src/store/modules/global.js`等关键文件,详细讲解了如何在登录后根据用户权限加载不同的菜单,包括`addRoutes`动态添加路由和在`login/index.vue`、`router/modules/privilege.js`、`router/modules/Jenkins.js`中设置权限逻辑。

src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {
   
    getToken } from '@/utils/auth'
import getPageTitle from '@/utils/get-page-title'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [  //必有路由,不用设置权限的路由
  {
   
   
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
   
   
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
   
   
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
   
   
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
   
   
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
   
   
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
   
   
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
   
   
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: {
   
    title: 'dashboard', icon: 'dashboard', premissiong: '/dashboard/index', affix: true }
      }
    ]
  }
]

const createRouter = () => new Router({
   
   
  scrollBehavior: () => ({
   
    y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter () {
   
   
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// 捕获权限
router.beforeEach(async (to, from, next) => {
   
   
  const hasToken = getToken()
  console.log(to, 'to')
  console.log('hastoken', hasToken)
  NProgress.start(
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值