程序运行时,router只配置登陆 首页404 等基本页面

import Main from '@/views/Main.vue';

// 不作为Main组件的子页面展示的页面单独写,如下
export const loginRouter = {
path: '/login',
name: 'login',
meta: {
title: 'Login - 登录'
},
component: resolve => { require(['@/views/login.vue'], resolve); }
}; export const page404 = {
path: '/*',
name: 'error-404',
meta: {
title: '404-页面不存在'
},
component: resolve => { require(['@/views/error-page/404.vue'], resolve); }
}; export const page403 = {
path: '/403',
meta: {
title: '403-权限不足'
},
name: 'error-403',
component: resolve => { require(['@//views/error-page/403.vue'], resolve); }
}; export const page500 = {
path: '/500',
meta: {
title: '500-服务端错误'
},
name: 'error-500',
component: resolve => { require(['@/views/error-page/500.vue'], resolve); }
}; // 作为Main组件的子页面展示但是不在左侧菜单显示的路由写在otherRouter里
export const otherRouter = {
path: '/',
component: Main,
children: [
{ path: '/', title:'Home', name: 'home_index', component: resolve => { require(['@/views/page/home.vue'], resolve); } }
]
}; // 作为Main组件的子页面展示并且在左侧菜单显示的路由写在appRouter里
export const appRouter = []; // 所有上面定义的路由都要写在下面的routers里
export const routers = [
loginRouter,
otherRouter,
...appRouter,
page500,
page403
];

使用路由

import Vue from 'vue';
import iView from 'iview';
import Util from '../libs/util';
import VueRouter from 'vue-router';
import {routers,page404} from './router';
import Cookies from 'js-cookie';
import Main from '@/views/Main.vue';
import store from '@/store'; Vue.use(VueRouter); // 路由配置
const RouterConfig = {
// mode: 'history',
routes: routers
}; export const router = new VueRouter(RouterConfig); router.beforeEach((to, from, next) => {
iView.LoadingBar.start();
Util.title(to.meta.title);
if (!Cookies.get('user') && to.name !== 'login') { // 判断是否已经登录且前往的页面不是登录页
next({
name: 'login'
});
} else if (Cookies.get('user') && to.name === 'login') { // 判断是否已经登录且前往的是登录页
Util.title();
next({
name: '/'
});
} else {
next();
}
}); router.afterEach((to) => {
iView.LoadingBar.finish();
window.scrollTo(0, 0);
});

登陆操作 获取菜单 将菜单存到sessionStorage中,并且把菜单存储到vuex中,因为vuex一刷新就没了,所以要放在sessionStorage中

handleSubmit() {
this.$refs.loginForm.validate((valid) => {
if (valid) {
this.$http.post('/api/login/login', this.form)
.then((data) => {
this.submitPedding = false
if (data.result) {
window.sessionStorage.clear();
this.$http.get('/api/Common/GetMenuTree')
.then((data) => {
window.sessionStorage.routes = JSON.stringify(data)
this.$store.commit('updateMenulist', data)
// let routes = this.generateRoutesFromMenu(data)
// for(var i = 0;i<routes.length;i++){
// this.$router.options.routes.push(routes[i]);
// }
// this.$router.addRoutes(this.$router.options.routes) Cookies.set('user', this.form.userName);
this.$router.push({
path: '/'
});
})
} else {
switch (data.errorcode) {
case 1:
this.$Message.info({
content: '请输入您的账号',
duration: 10,
closable: true
});
break;
case 2:
this.$Message.info({
content: '请输入您的密码',
duration: 10,
closable: true
});
break;
case 3:
this.$Message.warning({
content: '用户名或密码错误',
duration: 10,
closable: true
});
break;
default:
this.$Message.info({
content: '登陆失败,请稍后再试',
duration: 10,
closable: true
});
break;
}
}
}) }
});
},

返回格式:

{
"results": false,
"code": 0,
"msg": null,
"sub_code": null,
"sub_msg": null,
"data": [{
"path": "/ParcelReceiving",
"icon": "earth",
"title": "包裹收货",
"component": null,
"children": [{
"path": "/ParcelReceiving/index",
"title": "Page",
"name": "opbgsh1",
"component": "ParcelReceiving"
}]
}, {
"path": "/ParcelProcessing",
"icon": "social-buffer",
"title": "包裹处理",
"component": null,
"children": [{
"path": "/ParcelProcessing/index",
"title": "Page",
"name": "opbgcl1",
"component": "ParcelProcessing"
}]
}, {
"path": "/CustomsEditor",
"icon": "clipboard",
"title": "海关信息",
"component": null,
"children": [{
"path": "/CustomsEditor/index",
"title": "Page",
"name": "ophgxx1",
"component": "CustomsEditor"
}]
}, {
"path": "/ParcelTransfer",
"icon": "ios-infinite",
"title": "包裹转单",
"component": null,
"children": [{
"path": "/ParcelTransfer/index",
"title": "Page",
"name": "opbgzd1",
"component": "ParcelTransfer"
}]
}, {
"path": "/PackagingServices",
"icon": "archive",
"title": "打包封装",
"component": null,
"children": [{
"path": "/PackagingServices/index",
"title": "Page",
"name": "opdbfz1",
"component": "PackagingServices"
}]
}, {
"path": "/ProblemPackageList",
"icon": "alert-circled",
"title": "问题包裹",
"component": null,
"children": [{
"path": "/ProblemPackageList/index",
"title": "Page",
"name": "opwtbg1",
"component": "ProblemPackageList"
}]
}, {
"path": "/ParcelSearch",
"icon": "search",
"title": "包裹查询",
"component": null,
"children": [{
"path": "/ParcelSearch/index",
"title": "Page",
"name": "opbgcx1",
"component": "ParcelSearch"
}]
}, {
"path": "/TrayInquire",
"icon": "search",
"title": "托盘查询",
"component": null,
"children": [{
"path": "/TrayInquire/index",
"title": "Page",
"name": "optpcx1",
"component": "TrayInquire"
}]
}, {
"path": "/TransferLabel",
"icon": "ios-infinite",
"title": "转运单",
"component": null,
"children": [{
"path": "/TransferLabel/index",
"title": "Page",
"name": "opbgzyd1",
"component": "TransferLabel"
}]
}, {
"path": "/CategoryName",
"icon": "sad-outline",
"title": "品类管理",
"component": null,
"children": [{
"path": "/CategoryName/index",
"title": "品类列表",
"name": "opplgl1",
"component": "CategoryName"
}, {
"path": "/CategoryName/Add",
"title": "添加品类",
"name": "opplgl2",
"component": "CategoryNameAdd"
}, {
"path": "/CategoryName/Prohibited",
"title": "禁运品名",
"name": "opplgl3",
"component": "ProhibitedCategory"
}]
}, {
"path": "/SystemPlugs",
"icon": "android-download",
"title": "系统设置",
"component": null,
"children": [{
"path": "/SystemPlugs/index",
"title": "Page",
"name": "opxtsz1",
"component": "SystemPlugs"
}]
}]
}

其中component存储的是字符串 需要转换为引入 vue组件 修改router.index 在使用前 修改配置

import Vue from 'vue';
import iView from 'iview';
import Util from '../libs/util';
import VueRouter from 'vue-router';
import {routers,page404} from './router';
import Cookies from 'js-cookie';
import Main from '@/views/Main.vue';
import store from '@/store'; Vue.use(VueRouter); // 路由配置
const RouterConfig = {
// mode: 'history',
routes: routers
}; //避免刷新后 菜单清空
let menus = window.sessionStorage.routes //登录成功返回的菜单
if (menus != null && menus != "null" && menus.length != 0) {
let items = JSON.parse(menus)
store.commit('updateMenulist', items)
let routes = generateRoutesFromMenu(items)
for(var i = 0;i<routes.length;i++){
routers.push(routes[i]);
}
}
routers.push(page404)
function generateRoutesFromMenu(menu = [], routes = []) {
for (let i = 0; i < menu.length; i++) {
menu[i].component = require('../views/Main.vue');
for(let j = 0;j<menu[i].children.length;j++){
let filePath = menu[i].children[j].component
menu[i].children[j].component = require('../views/page/' + filePath + '.vue');
}
routes.push(menu[i])
}
return routes
}
export const router = new VueRouter(RouterConfig); router.beforeEach((to, from, next) => {
iView.LoadingBar.start();
Util.title(to.meta.title);
if (!Cookies.get('user') && to.name !== 'login') { // 判断是否已经登录且前往的页面不是登录页
next({
name: 'login'
});
} else if (Cookies.get('user') && to.name === 'login') { // 判断是否已经登录且前往的是登录页
Util.title();
next({
name: '/'
});
} else {
next();
}
}); router.afterEach((to) => {
iView.LoadingBar.finish();
window.scrollTo(0, 0);
});
menu[i].children[j].component = require('../views/page/' + filePath + '.vue'); 这里 前面的路径 和后面的.vue不可少,意思是让webpack到views下面的page下面去找.vue结尾的文件。
404路由 要加到最后 否则路由先找到*的 以后的就不执行了。
store中
const app = {
state: {
menuList: [],
routers: [
otherRouter,
...appRouter
]
},
mutations: {
updateMenulist (state,menu) {
state.menuList = menu;
}
}
};

左侧菜单遍历 store中的menuList

<template>
<div>
<template v-for="(item, index) in menuList">
<div style="text-align: center;" :key="index">
<Dropdown transfer v-if="item.children.length !== 1" placement="right-start" :key="index" @on-click="changeMenu">
<Button style="width: 70px;margin-left: -5px;padding:10px 0;" type="text">
<Icon :size="20" :color="iconColor" :type="item.icon"></Icon>
</Button>
<DropdownMenu style="width: 200px;" slot="list">
<template v-for="(child, i) in item.children">
<DropdownItem :name="child.name" :key="i"><Icon :type="child.icon"></Icon><span style="padding-left:10px;">{{ itemTitle(child) }}</span></DropdownItem>
</template>
</DropdownMenu>
</Dropdown>
<Dropdown transfer v-else placement="right-start" :key="index" @on-click="changeMenu">
<Button @click="changeMenu(item.children[0].name)" style="width: 70px;margin-left: -5px;padding:10px 0;" type="text">
<Icon :size="20" :color="iconColor" :type="item.icon"></Icon>
</Button>
<DropdownMenu style="width: 200px;" slot="list">
<DropdownItem :name="item.children[0].name" :key="'d' + index"><Icon :type="item.icon"></Icon><span style="padding-left:10px;">{{ itemTitle(item.children[0]) }}</span></DropdownItem>
</DropdownMenu>
</Dropdown>
</div>
</template>
</div>
</template>

至此 权限从后台读取,到前台页面展示就完成了,页面内部每次刷新 都会重新从sessionStorage中获取重新赋值。

Vuejs 实现权限管理的更多相关文章

  1. Android权限管理之RxPermission解决Android 6.0 适配问题

    前言: 上篇重点学习了Android 6.0的运行时权限,今天还是围绕着Android 6.0权限适配来总结学习,这里主要介绍一下我们公司解决Android 6.0权限适配的方案:RxJava+RxP ...

  2. Android权限管理之Android 6.0运行时权限及解决办法

    前言: 今天还是围绕着最近面试的一个热门话题Android 6.0权限适配来总结学习,其实Android 6.0权限适配我们公司是在今年5月份才开始做,算是比较晚的吧,不过现在Android 6.0以 ...

  3. Android权限管理之Permission权限机制及使用

    前言: 最近突然喜欢上一句诗:"宠辱不惊,看庭前花开花落:去留无意,望天空云卷云舒." 哈哈~,这个和今天的主题无关,最近只要不学习总觉得生活中少了点什么,所以想着围绕着最近面试过 ...

  4. SpringMVC+Shiro权限管理【转】

    1.权限的简单描述 2.实例表结构及内容及POJO 3.Shiro-pom.xml 4.Shiro-web.xml 5.Shiro-MyShiro-权限认证,登录认证层 6.Shiro-applica ...

  5. Android6.0运行时权限管理

    自从Android6.0发布以来,在权限上做出了很大的变动,不再是之前的只要在manifest设置就可以任意获取权限,而是更加的注重用户的隐私和体验,不会再强迫用户因拒绝不该拥有的权限而导致的无法安装 ...

  6. Oracle 表空间和用户权限管理

    一. 表空间 Oracle数据库包含逻辑结构和物理结构. 数据库的物理结构指的是构成数据库的一组操作系统文件. 数据库的逻辑结构是指描述数据组织方式的一组逻辑概念以及它们之间的关系. 表空间是数据库逻 ...

  7. [Django]用户权限学习系列之权限管理界面实现

    本系列前三章: http://www.cnblogs.com/CQ-LQJ/p/5604331.htmlPermission权限基本操作指令 http://www.cnblogs.com/CQ-LQJ ...

  8. [Django]用户权限学习系列之设计自有权限管理系统设计思路

    若在阅读本片文章遇到权限操作问题,请查看本系列的前两章! http://www.cnblogs.com/CQ-LQJ/p/5609690.html和http://www.cnblogs.com/CQ- ...

  9. 我的MYSQL学习心得(十三) 权限管理

    我的MYSQL学习心得(十三) 权限管理 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) ...

随机推荐

  1. c# 类的初步认识

    这里我们把类分为三种: String类(字符串类):Math类(数学类):DateTime类(时间日期类). 在使用类时注意  在输入的过程中代码前面会出现一些符号(紫色立方体代表方法,函数和黑色扳手 ...

  2. c#循环语句 for 循环嵌套的练习。还有跳转语句,异常语句,迭代穷举介绍

    先说一下循环嵌套:循环嵌套就是再一个循环里面再放一个循环,也就是说如果没一个循环都循环10次,那么第一个循环是1的时候,嵌套的循环会循环十次.也就是10*10的效果. for 循环语句 主要还是逻辑思 ...

  3. Centos7安装mysql缺乏yum源怎么安装

    找到mysql5.6的centos的repo源,终于解决mysql的安装问题: 1.确保centos安装了wget,没有的话安装wget   1 yum install wget 2.下载mysql的 ...

  4. Mat类的输出格式

    从前面的例程中, 可以看到 Mat 类重载了<<操作符, 可以方便得使用流操作来输出矩阵的内容.默认情况下输出的格式是类似 Matlab 中矩阵的输出格式.除了默认格式,Mat 也支持其他 ...

  5. Python程序设计9——数据库编程

    1 数据持久化 持久化是将内存中的对象存储在关系数据库中,当然也可以存储在磁盘文件.XML数据文件中.实现数据持久化至少需要实现以下3个接口 void Save(object o):把一个对象保存到外 ...

  6. JavaWeb_内省(Instrospector)

    内省是什么? 开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都是用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作Java对象的属性. 什么是Java对象的 ...

  7. leetcode Word Search 待解决?

    终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞 ...

  8. JSP或者说是JSP翻译引擎

    JSP最终输出就是底层去继承Servlet,然后输出JSP页面上的内容而已,不明白了看第六条. 页面java脚本,也就是JSP页面上写JAVA代码如下:  

  9. Algorithms - Quick Sort

    印象 图2 快速排序过程 思想 通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序的目的. 分析 稳定: ...

  10. 企业sudo权限规划详解 (实测一个堆命令搞定)

    简述问题:         随着公司的服务器越来越多,人员流动性也开始与日俱增,以往管理服务器的陈旧思想应当摒弃,公司需要有 更好更完善的权限体系,经过多轮沟通和协商,公司一致决定重新整理规划权限体系 ...