最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
vue动态设置路由权限的主要思路代码
时间:2021-01-13 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下vue动态设置路由权限的主要思路代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
主要思路就是:
1.配置路由的时候绑定好id,可后端开发完成后,与后端同步id就行,这id唯一不变,根据此id可找到路由地址及icon。
const routerArr = [ { path: '', name: '', component: () => import( /* webpackChunkName: "strategiesMaintain" */ '@/components/Layout/Index'), meta: { requireAuth: true, id: 1, icon: 'iconzhanghuguanli', title: '路由1' }, children: [{ path: '/verificationLog', name: 'VerificationLog', component: () => import( /* webpackChunkName: "verificationLog" */ '@/views/auditManage/verificationLog'), meta: { requireAuth: true, id: 101, icon: 'icon-disanfangyanzhengrizhi', title: '路由11' } }, { path: '/systemLog', name: 'SystemLog', component: () => import( /* webpackChunkName: "systemLog" */ '@/views/auditManage/systemLog'), meta: { requireAuth: true, id: 102, icon: 'icon-xitongcaozuorizhi', title: '路由12' } }] } ]; export default routerArr;
2.设置本地路由与后端传来的路由的联系,主要是根据id绑定路由地址及iconClass
import routerModules from "@/router/modules"; import {http} from '@/utils/http' import store from '@/store'; import { Message } from 'element-ui' const formateResData = (val) =>{ // 格式化路由数据 const obj = {}; const fn = (arr)=>{ for(let i = 0,item;item = arr[i++];){ obj[item['meta']['id']] = { path: item['path'], iconClass: item['meta']['icon'] }; if(item.children && item.children.length > 0){ fn(item.children); } } } fn(val); return obj; }; const MAPOBJ = formateResData(routerModules); const dealWithData = (navData) => { // 处理菜单数据 let firstLink = ""; const navIdArr = []; const fn = (arr) => { for (let i = 0,item;item = arr[i++];) { item['iconClass'] = MAPOBJ[item.id].iconClass; item['linkAction'] = MAPOBJ[item.id].path; navIdArr.push(item.id); if (!firstLink && !item.subMenu) { // 设置默认跳转 firstLink = item['linkAction']; } if (item.subMenu && item.subMenu.length > 0) { fn(item.subMenu); } } } fn(navData); return {navData,navIdArr,firstLink}; }; let navIds = []; const getNav = async (to={},from={},next=()=>{})=>{ // 获取导航数据 const {code,data} = await http("/menu/list", {}, "GET"); // 获取菜单数据 // const data = require("@/mock/api/menuData"); // 使用mock数据 const {navData,navIdArr,firstLink} = dealWithData(data); store.commit('setNavData', navData); navIds = navIdArr; if(to.fullPath == '/index'){ // 从登录过来 或者是回首页 next(firstLink); }else { // 刷新 if(navIds.indexOf(to.meta.id) == -1){ // 后端没有返回该菜单 Message.error('菜单不存在或者没有权限'); return; } next(); } } export const setGuard = (to={},from={},next=()=>{}) =>{ // 设置权限 if(navIds.length === 0){ // 还没有获取菜单数据 getNav(to,from,next); }else { // 获取到菜单数据 if(navIds.indexOf(to.meta.id) == -1){ // 后端没有返回该菜单 Message.error('菜单不存在或者没有权限'); return; } next(); } }
3.在mainjs中引入配置
router.beforeEach((to, from, next) => { let token = wlhStorage.get("authorization"); if (to.path == "/login") { storage.clear();// 清空缓存 next(); } else { if (to.meta.requireAuth && token) { // 登陆 setGuard(to,from,next); } else { // 没有登录 next("/login"); } } })
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24
- SpringBoot热部署设置详解 10-24
- JavaScript二叉搜索树构建操作解析 10-12
- JavaScript二叉树及各种遍历算法讲解 10-12