为了设置用户的访问权限,所以在一个vue工程中,往往需要前端和后端开发人员共同配合设置动态路由,

进而实现根据用户的权限来访问对应的模块

  1.首先在登录页面,前端跟据后端返回的路由信息进行配置,配置后存储到本地以便使用

login.vue页面
在methods中:
  //配置路由的方法
getMenuList(){
       let menuList = '后端给你返回的数据'
    let sysRouter = [];
let tempOne = {};
menuList.filter((menuOne, indexOne) => {
tempOne = {};
tempOne.title = menuOne.name; //这几个属性均为路由的一些配置项以及所用到的一些信息,根据实际情况进行编写
tempOne.icon = menuOne.icon;
tempOne.path = menuOne.perm;
tempOne.redirect = menuOne.url;
tempOne.children = [];
tempOne.component = Main; //Main是个文件 每个页面都依赖于这个文件
let tempTwo = {};
if (menuOne.children.length > ) {
menuOne.children.filter((menuTwo, indexTwo) => {
   tempTwo = {};
tempTwo.title = menuTwo.name;
tempTwo.path = menuTwo.url;
tempTwo.component = () => import(`@/pages${menuTwo.path}.vue`);
tempOne.children.push(tempTwo);
});
}
sysRouter.push(tempOne);
});
        sessionStorage.setItem("sysRouter", JSON.stringify(sysRouter));
    },
  //点击登录按钮的方法
  submitBtn(){
    let parm = {
                username: this.key,
                password: this.pwd,
             
            };
    api.login(parm).then(res => {
       if(res){
         //首先将你的所需要的信息存储到本地,例如token,用户信息的等
         //调用配置路由信息的方法
         //然后跳转到首页
         //最后刷新一下页面,不然会白屏 sessionStorage.setItem("loginToken", res.data.data.token);           this.getMenuList();
           this.$router.push({
                       path: "/home/home"
                  });
 
           setTimeout(()=>{
                       window.location.reload();
                  },50)
       }
    })

   }

接下来就是最关键的一步 配置 router里面的index.js文件

import Vue from 'vue';
import Router from 'vue-router';
import store from '../store';
import Main from '@/pages/Main.vue'; Vue.use(Router); /*
*
* 路由重复点击 警告问题解决
*
*/
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
} /*
*---- 登录前不运行,刷新时运行,用addRoutes加载路由 begin ----
*/
let systemRouter = [];
let localRouter = window.sessionStorage.getItem('sysRouter'); //用于刷新时加载导航 功能和addRoutes等同1
if (localRouter) {
//因为未登录 localRouter不存在,故不运行;只有刷新时才运行
let localJsonRouter = JSON.parse(localRouter);
systemRouter = localJsonRouter;
systemRouter.filter(item => {
//需要重新绑定 component
item.component = Main;
if (item.children.length > ) {
item.children.filter(itemTwo => {
itemTwo.component = () => import(`@/pages${itemTwo.path}.vue`);
});
}
});
} else {
systemRouter = [];
}
/*
*---- 登录前不运行,刷新时运行,用addRoutes加载路由 end ----
*/ // 定义其他路由 这一般都为3级路由
let otherRouter = [
{
path: '/menu2_0_1',
title: '设计数据管理',
icon: 'el-icon-s-opportunity',
redirect: '/designDataManage/clientRequireManage',
component: Main,
children: [
{
path: '/designDataManage/clientRequireManage/requireFileList',
title: '客户需求管理',
component: () =>
import(
'@/pages/designDataManage/requireFileList/requireFileList.vue'
)
},
{
path: '/designDataManage/designDataManageHome/designDataList',
title: '设计数据管理',
component: () =>
import(
'@/pages/designDataManage/designDataList/designDataList.vue'
)
},
{
path: '/designDataManage/designDataManageHome/FLDataList',
title: '设计数据管理',
component: () =>
import('@/pages/designDataManage/FLfileList/FLfileList.vue')
},
{
path:
'/designDataManage/designDataManageHome/workAfterFileList',
title: '设计数据管理',
component: () =>
import(
'@/pages/designDataManage/workAfterFileList/workAfterFileList.vue'
)
},
{
path: '/designDataManage/designDataManageHome/numFileList',
title: '设计数据管理',
component: () =>
import(
'@/pages/designDataManage/numFileList/numFileList.vue'
)
},
{
path: '/designDataManage/printFileManage/printFileList',
title: '制版文件管理',
component: () =>
import(
'@/pages/designDataManage/printFileList/printFileList.vue'
)
}
]
},
{
path: '/menu3_0_1',
title: '工艺数据管理',
icon: 'el-icon-s-open',
redirect: '/workDataMange/workDataMangeRepair',
component: Main,
children: [
{
path: '/workDataMange/workDataMangeRepair/workDataMangeList',
title: '客户需求管理',
component: () =>
import(
'@/pages/workDataMange/workDataManageList/workDataManageList.vue'
)
}
]
},
{
path: '/menu4_0_1',
title: '打样任务管理',
icon: 'el-icon-s-order',
redirect: '/printTaskManage/printTaskManageHome',
component: Main,
children: [
{
path:
'/printTaskManage/printTaskManageHome/printTaskManageList',
title: '编辑打样任务',
component: () =>
import(
'@/pages/printTaskManage/printTaskManageList/printTaskManageList.vue'
)
}
]
},
{
path: '/menu4_0_2',
title: '打样任务管理',
icon: 'el-icon-s-order',
redirect: '/printOAManage/printOAManageHome',
component: Main,
children: [
{
path: '/printOAManage/printOAManageHome/prinOAkManageList',
title: '编辑打样任务',
component: () =>
import(
'@/pages/printTaskManage/printOAmanageList/printOAmanageList.vue'
)
},
{
path:
'/printOAManage/printOAManageHome/prinOAkManageStartWorkList',
title: '开工',
component: () =>
import(
'@/pages/printTaskManage/prinOAkManageStartWorkList/prinOAkManageStartWorkList.vue'
)
},
{
path: '/printOAManage/printOAManageHome/paraDetail',
name: 'paramaDetail',
title: '工艺参数详情',
component: () =>
import(
'@/pages/printTaskManage/prinOAkManageStartWorkList/paramaDetail.vue'
)
}
]
},
{
path: '/menu5_0_2',
title: '打样基线管理',
icon: 'el-icon-s-order',
redirect: '/printBasicLineManage/importantColorData',
component: Main,
children: [
{
path:
'/printBasicLineManage/importantColorData/importantColorDataList',
title: '关键颜色详情',
component: () =>
import(
'@/pages/printBasicLineManage/importantColorDataList/importantColorDataList.vue'
)
}
]
}
]; //声明路由对象,实例化VueRouter
const router = new Router({
routes: [
//登录路由
{
path: '/',
name: 'login',
component: () => import('@/pages/login.vue')
},
{
path: '/error',
name: 'error',
component: () => import('@/pages/error.vue')
},
{
path: '/resetPassword',
name: 'resetPassword',
component: () => import('@/pages/checkPassword/resetPassword.vue')
},
{
path: '/checkNameAndEmail',
name: 'checkNameAndEmail',
component: () => import('@/pages/checkPassword/checkNameAndEmail.vue')
},
...systemRouter,
...otherRouter
]
}); /* * 加载页面之前 钩子函数 * 使用场景:一般用在跳转前需要做校验的地方
* to:Route即将要进入的目标 路由对象;
* from:Route当前导航正要离开的路由;
* next:Function一定要调用该方法来resolve这个钩子。执行效果依赖next方法的调用参数。
*
*/
router.beforeEach(function (to, from, next) {
store.dispatch('updateActItem', to.path); /*--- 动态绑定路由格式 begin ---*/
let systemRouter = [];
let localRouter = window.sessionStorage.getItem('sysRouter'); //用于刷新时加载导航
//beforeEach,一步小心就进入到了他的死循环,浏览器都会崩亏,需要在一开始就加判断,拿到路由了,就直接next(),
if (localRouter) {
let localJsonRouter = JSON.parse(localRouter);
systemRouter = localJsonRouter;
systemRouter.filter(item => {
//需要重新绑定 component
item.component = Main;
if (item.children.length > ) {
item.children.filter(itemTwo => {
itemTwo.component = () =>
import(`@/pages${itemTwo.path}.vue`);
});
}
});
} else {
systemRouter = [];
}
if (window.sessionStorage.getItem('sysRouter') == null) {
//不加这个判断,路由会陷入死循环 !!!!
router.addRoutes(systemRouter); //动态加载路由
} store.dispatch('updateMenuList', systemRouter);
next();
/*--- 动态绑定路由格式 end ---*/
});
export default router;

  

vue --》动态路由的实现 (根据用户的权限来访问对应的模块)的更多相关文章

  1. vue动态路由

    我们经常需要把某种模式匹配到的所有路由,全都映射到同个组件.例如,我们有一个 User 组件,对于所有 ID 各不相同的用户,都要使用这个组件来渲染.能够提供参数的路由即为动态路由第一步:定义组件 c ...

  2. Vue 动态路由的实现以及 Springsecurity 按钮级别的权限控制

    思路: 动态路由实现:在导航守卫中判断用户是否有用户信息,通过调用接口,拿到后台根据用户角色生成的菜单树,格式化菜单树结构信息并递归生成层级路由表并使用Vuex保存,通过 router.addRout ...

  3. vue——动态路由以及地址传参

    动态路由: 当我们很多个页面或者组件都要被很多次重复利用的时候,我们的路由都指向同一个组件,这时候从不同组件进入一个”共用”的组件,并且还要传参数,渲染不同的数据 这就要用到动态路由跟路由传参了! 1 ...

  4. 18 vue 动态路由传参

    params形式 http://192.168.1.100:8080/#/infoDetailed/231 //定义路由{ path: "/infoDetailed/:newsId" ...

  5. Vue 动态路由传值

    一.动态路由传值 1.配置动态路由: const routes = [ //动态路由路径参数以:开头 { path: '/Content/:aid', component:Content}, ] 2. ...

  6. vue 动态路由 Get传值

    main.js //2.配置路由 注意:名字 const routes = [ { path: '/home', component: Home }, { path: '/news', compone ...

  7. vue动态路由配置,vue路由传参

    动态路由: 当我们很多个页面或者组件都要被很多次重复利用的时候,我们的路由都指向同一个组件,这时候从不同组件进入一个"共用"的组件,并且还要传参数,渲染不同的数据 这就要用到动态路 ...

  8. Vue动态路由 Get传值

    <template> <!-- 所有的内容要被根节点包含起来 --> <div id="home"> 我是首页组件 <ul> < ...

  9. vue动态路由传值以及get传值及编程式导航

    1.动态路由传值 1.配置路由处 { path: '/content/:id', component: Content }, // 动态路由 2.对应页面传值 <router-link :to= ...

随机推荐

  1. 零拷贝的原理及Java实现

    在谈论Kafka高性能时不得不提到零拷贝.Kafka通过采用零拷贝大大提供了应用性能,减少了内核和用户模式之间的上下文切换次数.那么什么是零拷贝,如何实现零拷贝呢? 什么是零拷贝 WIKI中对其有如下 ...

  2. public、protected、private

    public(公有):可以在任何地方被访问 protected(受保护):可以被其自身以及其子类和父类访问,类的对象也不可以访问 private(私有):只能被其定义所在的类访问,类的对象也不可以访问 ...

  3. 【leetcode】1267. Count Servers that Communicate

    题目如下: You are given a map of a server center, represented as a m * n integer matrix grid, where 1 me ...

  4. Ubuntu:打开JPEG文件错误(Not a JPEG File: starts with 0x52 0x49)

    Ubuntu 16.04.4,造冰箱的大熊猫@cnblogs 2018/7/12 近日下载资料时得到一些后缀为jpg的图片文件.这些图片在手机上能够正常预览,但在Ubuntu的文件管理器中无法预览这些 ...

  5. sh_08_格式化字符串

    sh_08_格式化字符串 info_tuple = ("小明", 21, 1.85) # 格式化字符串后面的 `()` 本质上就是元组 print("%s 年龄是 %d ...

  6. Ubuntu访问samba共享文件方法

    1.1  安装samba客户端 sudo apt-get install smbclient -y 1.2  查看文件共享权限 smbclient -L //192.168.100.6 1.3  创建 ...

  7. 利用MFC在控件内将txt中的数据画图

    1:采集txt文件中的数据测试程序如下: #include "stdafx.h" #include <fstream> #include "iostream& ...

  8. Java密码处理

  9. What is the !! (not not) operator in JavaScript?

    What is the !! (not not) operator in JavaScript? 解答1 Coerces强制 oObject to boolean. If it was falsey ...

  10. Servlet基础总结

    1.Servlet概念: Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间 ...