vue中怎么根据用户权限动态添加路由
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍"vue中怎么根据用户权限动态添加路由",在日常操作中,相信很多人在vue中怎么根据用户权限动态添加路由问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"v
千家信息网最后更新 2026年11月02日vue中怎么根据用户权限动态添加路由知识点
这篇文章主要介绍"vue中怎么根据用户权限动态添加路由",在日常操作中,相信很多人在vue中怎么根据用户权限动态添加路由问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"vue中怎么根据用户权限动态添加路由"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
知识点
路由守卫(使用了前置守卫):根据用户角色判断要添加的路由
vuex:保存动态添加的路由
难点
每次路由发生变化时都需要调用一次路由守卫,并且store中的数据会在每次刷新的时候清空,因此需要判断store中是否有添加的动态路由。
(若没有判断 则会一直添加 导致内存溢出)
根据角色判断路由
过滤动态路由 判断每条路由角色是否与登录传入的角色一致
{{ item.meta.title }} {{ chi.meta.title }}
import Vue from 'vue'import VueRouter from 'vue-router'import store from '../store/index'Vue.use(VueRouter)const originalPush = VueRouter.prototype.pushVueRouter.prototype.push = function push(location) { return originalPush.call(this, location).catch(err => err)}export const routes = [ { path: '/home', name: 'First', component: () => import('../views/Index.vue'), meta: { title: 'Home'}, children: [ { path: 'index', name: 'Home', component: () => import('../views/Home'), meta: { title: 'Home', roles: ['Customer'] } } ] }, { path: '/index', name: 'NavigationOne', component: () => import('../views/Index.vue'), meta: { title: '导航一'}, children: [ { path: 'personnel', name: 'Personnel ', component: () => import('../views/One/Personnel.vue'), meta: { title: 'Personnel', roles: ['Customer'] } }, { path: 'account', name: 'Account', component: () => import('../views/One/Account.vue'), meta: { title: 'Account', roles: ['Customer'] } }, { path: 'psw', name: 'psw', component: () => import('../views/One/Password.vue'), meta: { title: 'psw', roles: ['Customer'] } } ] }, { path: '/card', name: 'NavigationTwo', component: () => import('../views/Index.vue'), meta: { title: '导航二'}, children: [ { path: 'activity', name: 'Activity ', component: () => import('../views/Three/Activity.vue'), meta: { title: 'Activity', roles: ['Customer'] } }, { path: 'Social', name: 'Social', component: () => import('../views/Three/Social.vue'), meta: { title: 'Social', roles: ['Customer'] } }, { path: 'content', name: 'Content', component: () => import('../views/Three/Content.vue'), meta: { title: 'Content', roles: ['Customer'] } } ] }, { path: '/two', name: 'NavigationThree', component: () => import('../views/Index.vue'), meta: { title: '导航三'}, children: [ { path: 'index', name: 'Two ', component: () => import('../views/Two'), meta: { title: 'Two', roles: ['Customer'] } }] }, { path: '/404', name: 'Error', hidden: true, meta: { title: 'error'}, component: () => import('../views/Error') }]export const asyncRouter = [ // Agent3 Staff2 { path: '/agent', component: () => import('../views/Index.vue'), name: 'Agent', meta: { title: 'Agent', roles: ['Agent','Staff']}, children: [ { path: 'one', name: 'agentOne', component: () => import('@/views/agent/One'), meta: { title: 'agentOne', roles: ['Agent','Staff'] } }, { path: 'two', name: 'agentTwo', component: () => import('@/views/agent/Two'), meta: { title: 'agentTwo', roles: ['Agent'] } }, { path: 'three', name: 'agentThree', component: () => import('@/views/agent/Three'), meta: { title: 'agentThree', roles: ['Agent','Staff'] } } ] }, // Staff3 { path: '/staff', component: () => import('../views/Index.vue'), name: 'Staff', meta: { title: 'Staff', roles: ['Staff']}, children: [ { path: 'one', name: 'StaffOne', component: () => import('@/views/Staff/One'), meta: { title: 'StaffOne', roles: ['Staff'] } }, { path: 'two', name: 'StaffTwo', component: () => import('@/views/Staff/Two'), meta: { title: 'StaffTwo', roles: ['Staff'] } }, { path: 'three', name: 'StaffThree', component: () => import('@/views/Staff/Three'), meta: { title: 'StaffThree', roles: ['Staff'] } } ] }, { path: '*', redirect: '/404', hidden: true }]const router = new VueRouter({ routes})router.beforeEach((to, from, next) =>{ let roles = ['Staff'] if(store.state.Routers.length) { console.log('yes') next() } else { console.log('not') store.dispatch('asyncGetRouter', {roles}) .then(res =>{ router.addRoutes(store.state.addRouters) }) next({...to}) // next()与next({ ...to })的区别:next() 放行 next('/XXX') 无限拦截 }})export default routerimport Vue from 'vue'import Vuex from 'vuex'import modules from './module'import router, {routes, asyncRouter} from '../router'function hasPermission(route, roles) { if(route.meta && route.meta.roles) { return roles.some(role =>route.meta.roles.indexOf(role) >= 0) } else { return true } }/* 递归过滤异步路由表 返回符合用户角色的路由 @param asyncRouter 异步路由 @param roles 用户角色*/function filterAsyncRouter(asyncRouter, roles) { let filterRouter = asyncRouter.filter(route =>{ if(hasPermission(route, roles)) { if(route.children && route.children.length) { route.children = filterAsyncRouter(route.children, roles) } return true } return false }) return filterRouter}Vue.use(Vuex)export default new Vuex.Store({ state: { addRouters: [], Routers: [] }, mutations: { getRouter(state, paload) { // console.log(paload) state.Routers = routes.concat(paload) state.addRouters = paload // router.addRoutes(paload) } }, actions: { asyncGetRouter({ commit }, data) { const { roles } = data return new Promise(resolve =>{ let addAsyncRouters = filterAsyncRouter(asyncRouter, roles) commit('getRouter', addAsyncRouters) resolve() }) } }})到此,关于"vue中怎么根据用户权限动态添加路由"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
路由
动态
用户
角色
权限
学习
导航
更多
知识
帮助
实用
一致
接下来
内存
数据
文章
方法
时候
理论
知识点
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网金融科技
广州为想互联网科技有限公司
互联网科技保险
麻工互联网科技
互联网 的科技创新
科技互联网公司logo
郑州华宇互联网科技
佰商互联网科技有限公司
中国最先进互联网科技公司
农村开互联网科技讲堂
以后互联网后是啥科技
互联网科技项目计划书材料类
宜昌元轩互联网科技有限公司
链环科技传统企业怎么转型互联网
内蒙古九一互联网科技
中和天地互联网科技有限公司
富国互联网金融科技混合基金
互联网科技快讯热点
宁波跨境商互联网科技有限公司
济南小医互联网科技有限公司招聘
咖啡网
互联网科技网
圆梦互联网科技
科技服务与互联网
未来的互联网科技
温州喔噻互联网科技有限公司
承德科技产业互联网发展
速维有品互联网信息科技(苏州)有限公司
互联网科技圈年度人物
江苏互联网科技小贷