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

  1. import Main from '@/views/Main.vue';
  2.  
  3. // 不作为Main组件的子页面展示的页面单独写,如下
  4. export const loginRouter = {
  5. path: '/login',
  6. name: 'login',
  7. meta: {
  8. title: 'Login - 登录'
  9. },
  10. component: resolve => { require(['@/views/login.vue'], resolve); }
  11. };
  12.  
  13. export const page404 = {
  14. path: '/*',
  15. name: 'error-404',
  16. meta: {
  17. title: '404-页面不存在'
  18. },
  19. component: resolve => { require(['@/views/error-page/404.vue'], resolve); }
  20. };
  21.  
  22. export const page403 = {
  23. path: '/403',
  24. meta: {
  25. title: '403-权限不足'
  26. },
  27. name: 'error-403',
  28. component: resolve => { require(['@//views/error-page/403.vue'], resolve); }
  29. };
  30.  
  31. export const page500 = {
  32. path: '/500',
  33. meta: {
  34. title: '500-服务端错误'
  35. },
  36. name: 'error-500',
  37. component: resolve => { require(['@/views/error-page/500.vue'], resolve); }
  38. };
  39.  
  40. // 作为Main组件的子页面展示但是不在左侧菜单显示的路由写在otherRouter里
  41. export const otherRouter = {
  42. path: '/',
  43. component: Main,
  44. children: [
  45. { path: '/', title:'Home', name: 'home_index', component: resolve => { require(['@/views/page/home.vue'], resolve); } }
  46. ]
  47. };
  48.  
  49. // 作为Main组件的子页面展示并且在左侧菜单显示的路由写在appRouter里
  50. export const appRouter = [];
  51.  
  52. // 所有上面定义的路由都要写在下面的routers里
  53. export const routers = [
  54. loginRouter,
  55. otherRouter,
  56. ...appRouter,
  57. page500,
  58. page403
  59. ];

使用路由

  1. import Vue from 'vue';
  2. import iView from 'iview';
  3. import Util from '../libs/util';
  4. import VueRouter from 'vue-router';
  5. import {routers,page404} from './router';
  6. import Cookies from 'js-cookie';
  7. import Main from '@/views/Main.vue';
  8. import store from '@/store';
  9.  
  10. Vue.use(VueRouter);
  11.  
  12. // 路由配置
  13. const RouterConfig = {
  14. // mode: 'history',
  15. routes: routers
  16. };
  17.  
  18. export const router = new VueRouter(RouterConfig);
  19.  
  20. router.beforeEach((to, from, next) => {
  21. iView.LoadingBar.start();
  22. Util.title(to.meta.title);
  23. if (!Cookies.get('user') && to.name !== 'login') { // 判断是否已经登录且前往的页面不是登录页
  24. next({
  25. name: 'login'
  26. });
  27. } else if (Cookies.get('user') && to.name === 'login') { // 判断是否已经登录且前往的是登录页
  28. Util.title();
  29. next({
  30. name: '/'
  31. });
  32. } else {
  33. next();
  34. }
  35. });
  36.  
  37. router.afterEach((to) => {
  38. iView.LoadingBar.finish();
  39. window.scrollTo(0, 0);
  40. });

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

  1. handleSubmit() {
  2. this.$refs.loginForm.validate((valid) => {
  3. if (valid) {
  4. this.$http.post('/api/login/login', this.form)
  5. .then((data) => {
  6. this.submitPedding = false
  7. if (data.result) {
  8. window.sessionStorage.clear();
  9. this.$http.get('/api/Common/GetMenuTree')
  10. .then((data) => {
  11. window.sessionStorage.routes = JSON.stringify(data)
  12. this.$store.commit('updateMenulist', data)
  13. // let routes = this.generateRoutesFromMenu(data)
  14. // for(var i = 0;i<routes.length;i++){
  15. // this.$router.options.routes.push(routes[i]);
  16. // }
  17. // this.$router.addRoutes(this.$router.options.routes)
  18.  
  19. Cookies.set('user', this.form.userName);
  20. this.$router.push({
  21. path: '/'
  22. });
  23. })
  24. } else {
  25. switch (data.errorcode) {
  26. case 1:
  27. this.$Message.info({
  28. content: '请输入您的账号',
  29. duration: 10,
  30. closable: true
  31. });
  32. break;
  33. case 2:
  34. this.$Message.info({
  35. content: '请输入您的密码',
  36. duration: 10,
  37. closable: true
  38. });
  39. break;
  40. case 3:
  41. this.$Message.warning({
  42. content: '用户名或密码错误',
  43. duration: 10,
  44. closable: true
  45. });
  46. break;
  47. default:
  48. this.$Message.info({
  49. content: '登陆失败,请稍后再试',
  50. duration: 10,
  51. closable: true
  52. });
  53. break;
  54. }
  55. }
  56. })
  57.  
  58. }
  59. });
  60. },

返回格式:

  1. {
  2. "results": false,
  3. "code": 0,
  4. "msg": null,
  5. "sub_code": null,
  6. "sub_msg": null,
  7. "data": [{
  8. "path": "/ParcelReceiving",
  9. "icon": "earth",
  10. "title": "包裹收货",
  11. "component": null,
  12. "children": [{
  13. "path": "/ParcelReceiving/index",
  14. "title": "Page",
  15. "name": "opbgsh1",
  16. "component": "ParcelReceiving"
  17. }]
  18. }, {
  19. "path": "/ParcelProcessing",
  20. "icon": "social-buffer",
  21. "title": "包裹处理",
  22. "component": null,
  23. "children": [{
  24. "path": "/ParcelProcessing/index",
  25. "title": "Page",
  26. "name": "opbgcl1",
  27. "component": "ParcelProcessing"
  28. }]
  29. }, {
  30. "path": "/CustomsEditor",
  31. "icon": "clipboard",
  32. "title": "海关信息",
  33. "component": null,
  34. "children": [{
  35. "path": "/CustomsEditor/index",
  36. "title": "Page",
  37. "name": "ophgxx1",
  38. "component": "CustomsEditor"
  39. }]
  40. }, {
  41. "path": "/ParcelTransfer",
  42. "icon": "ios-infinite",
  43. "title": "包裹转单",
  44. "component": null,
  45. "children": [{
  46. "path": "/ParcelTransfer/index",
  47. "title": "Page",
  48. "name": "opbgzd1",
  49. "component": "ParcelTransfer"
  50. }]
  51. }, {
  52. "path": "/PackagingServices",
  53. "icon": "archive",
  54. "title": "打包封装",
  55. "component": null,
  56. "children": [{
  57. "path": "/PackagingServices/index",
  58. "title": "Page",
  59. "name": "opdbfz1",
  60. "component": "PackagingServices"
  61. }]
  62. }, {
  63. "path": "/ProblemPackageList",
  64. "icon": "alert-circled",
  65. "title": "问题包裹",
  66. "component": null,
  67. "children": [{
  68. "path": "/ProblemPackageList/index",
  69. "title": "Page",
  70. "name": "opwtbg1",
  71. "component": "ProblemPackageList"
  72. }]
  73. }, {
  74. "path": "/ParcelSearch",
  75. "icon": "search",
  76. "title": "包裹查询",
  77. "component": null,
  78. "children": [{
  79. "path": "/ParcelSearch/index",
  80. "title": "Page",
  81. "name": "opbgcx1",
  82. "component": "ParcelSearch"
  83. }]
  84. }, {
  85. "path": "/TrayInquire",
  86. "icon": "search",
  87. "title": "托盘查询",
  88. "component": null,
  89. "children": [{
  90. "path": "/TrayInquire/index",
  91. "title": "Page",
  92. "name": "optpcx1",
  93. "component": "TrayInquire"
  94. }]
  95. }, {
  96. "path": "/TransferLabel",
  97. "icon": "ios-infinite",
  98. "title": "转运单",
  99. "component": null,
  100. "children": [{
  101. "path": "/TransferLabel/index",
  102. "title": "Page",
  103. "name": "opbgzyd1",
  104. "component": "TransferLabel"
  105. }]
  106. }, {
  107. "path": "/CategoryName",
  108. "icon": "sad-outline",
  109. "title": "品类管理",
  110. "component": null,
  111. "children": [{
  112. "path": "/CategoryName/index",
  113. "title": "品类列表",
  114. "name": "opplgl1",
  115. "component": "CategoryName"
  116. }, {
  117. "path": "/CategoryName/Add",
  118. "title": "添加品类",
  119. "name": "opplgl2",
  120. "component": "CategoryNameAdd"
  121. }, {
  122. "path": "/CategoryName/Prohibited",
  123. "title": "禁运品名",
  124. "name": "opplgl3",
  125. "component": "ProhibitedCategory"
  126. }]
  127. }, {
  128. "path": "/SystemPlugs",
  129. "icon": "android-download",
  130. "title": "系统设置",
  131. "component": null,
  132. "children": [{
  133. "path": "/SystemPlugs/index",
  134. "title": "Page",
  135. "name": "opxtsz1",
  136. "component": "SystemPlugs"
  137. }]
  138. }]
  139. }

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

  1. import Vue from 'vue';
  2. import iView from 'iview';
  3. import Util from '../libs/util';
  4. import VueRouter from 'vue-router';
  5. import {routers,page404} from './router';
  6. import Cookies from 'js-cookie';
  7. import Main from '@/views/Main.vue';
  8. import store from '@/store';
  9.  
  10. Vue.use(VueRouter);
  11.  
  12. // 路由配置
  13. const RouterConfig = {
  14. // mode: 'history',
  15. routes: routers
  16. };
  17.  
  18. //避免刷新后 菜单清空
  19. let menus = window.sessionStorage.routes //登录成功返回的菜单
  20. if (menus != null && menus != "null" && menus.length != 0) {
  21. let items = JSON.parse(menus)
  22. store.commit('updateMenulist', items)
  23. let routes = generateRoutesFromMenu(items)
  24. for(var i = 0;i<routes.length;i++){
  25. routers.push(routes[i]);
  26. }
  27. }
  28. routers.push(page404)
  29. function generateRoutesFromMenu(menu = [], routes = []) {
  30. for (let i = 0; i < menu.length; i++) {
  31. menu[i].component = require('../views/Main.vue');
  32. for(let j = 0;j<menu[i].children.length;j++){
  33. let filePath = menu[i].children[j].component
  34. menu[i].children[j].component = require('../views/page/' + filePath + '.vue');
  35. }
  36. routes.push(menu[i])
  37. }
  38. return routes
  39. }
  40. export const router = new VueRouter(RouterConfig);
  41.  
  42. router.beforeEach((to, from, next) => {
  43. iView.LoadingBar.start();
  44. Util.title(to.meta.title);
  45. if (!Cookies.get('user') && to.name !== 'login') { // 判断是否已经登录且前往的页面不是登录页
  46. next({
  47. name: 'login'
  48. });
  49. } else if (Cookies.get('user') && to.name === 'login') { // 判断是否已经登录且前往的是登录页
  50. Util.title();
  51. next({
  52. name: '/'
  53. });
  54. } else {
  55. next();
  56. }
  57. });
  58.  
  59. router.afterEach((to) => {
  60. iView.LoadingBar.finish();
  61. window.scrollTo(0, 0);
  62. });
  1. menu[i].children[j].component = require('../views/page/' + filePath + '.vue'); 这里 前面的路径 和后面的.vue不可少,意思是让webpackviews下面的page下面去找.vue结尾的文件。
    404路由 要加到最后 否则路由先找到*的 以后的就不执行了。
    store
  1. const app = {
  2. state: {
  3. menuList: [],
  4. routers: [
  5. otherRouter,
  6. ...appRouter
  7. ]
  8. },
  9. mutations: {
  10. updateMenulist (state,menu) {
  11. state.menuList = menu;
  12. }
  13. }
  14. };

左侧菜单遍历 store中的menuList

  1. <template>
  2. <div>
  3. <template v-for="(item, index) in menuList">
  4. <div style="text-align: center;" :key="index">
  5. <Dropdown transfer v-if="item.children.length !== 1" placement="right-start" :key="index" @on-click="changeMenu">
  6. <Button style="width: 70px;margin-left: -5px;padding:10px 0;" type="text">
  7. <Icon :size="20" :color="iconColor" :type="item.icon"></Icon>
  8. </Button>
  9. <DropdownMenu style="width: 200px;" slot="list">
  10. <template v-for="(child, i) in item.children">
  11. <DropdownItem :name="child.name" :key="i"><Icon :type="child.icon"></Icon><span style="padding-left:10px;">{{ itemTitle(child) }}</span></DropdownItem>
  12. </template>
  13. </DropdownMenu>
  14. </Dropdown>
  15. <Dropdown transfer v-else placement="right-start" :key="index" @on-click="changeMenu">
  16. <Button @click="changeMenu(item.children[0].name)" style="width: 70px;margin-left: -5px;padding:10px 0;" type="text">
  17. <Icon :size="20" :color="iconColor" :type="item.icon"></Icon>
  18. </Button>
  19. <DropdownMenu style="width: 200px;" slot="list">
  20. <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>
  21. </DropdownMenu>
  22. </Dropdown>
  23. </div>
  24. </template>
  25. </div>
  26. </template>

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

  1.  

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. MyEclipse 报错:Errors running builder 'DeploymentBuilder' on project '工程名'

    并没有更换MyEclipse版本,只是重新卸载了下,然后就报错误,参考了网上的文章 解决版本 .就是删除工程下部署文件

  2. [问与答]Python 中 __all__ 的作用 ?

    你要是看Python的源码或者相关框架的源码,总是在 __init__.py 或者是源文件的开头看到一个 __all__ 变量的定义,今天就说说它的作用. orangleliu 问题出处 Can so ...

  3. linux测试noatime对文件访问时间的影响

    linux测试noatime对文件访问时间的影响 文件(如abc)有3个时间: # stat abc Access: 2015-04-16 19:30:13.665970572 +0800 Modif ...

  4. STL算法设计理念 - 谓词,一元谓词demo

    谓词: 一元函数对象:函数参数1个: 二元函数对象:函数参数2个: 一元谓词 函数参数1个,函数返回值是bool类型,可以作为一个判断式 谓词可以使一个仿函数,也可以是一个回调函数. demo 一元谓 ...

  5. session效率

    (1)-不恰当的request.getSession() 在HttpServlet中,HttpSession对象通常在request.getSession(true)方法调用时才创建. HttpSes ...

  6. 【60】Spring总结之基础架构(1)

    为什么用Spring? Spring一直贯彻并遵守"好的设计优于具体实现,代码应易于测试",这一理念,并最终带给我们一个易于开发.便于测试而又功能齐全的开发框架.概括起来Sprin ...

  7. 4.windows环境下如何安装memcached教程(转载+自己整理)

     Memcached 是一个开源免费高性能的分布式内存对象缓存系统,能够加快网站访问速度和减轻数据库压力,本文向大家介绍下windows环境下如何安装memcached.百度经验:jingyan. ...

  8. ExtJS:GridPanel之renderer:function()和itemdblclick : function()方法参数详解

    要使用GridPanel,首先要定义Store,而在创建Store的时候必须要有Model,因此我们首先来定义Model: Ext.define("Gpsdata", { exte ...

  9. C语言之插入排序

    插入法排序的要领就是每读入一个数立即插入到最终存放的数组中,每次插入都使得该数组有序. 上代码: #include <stdio.h> #include <stdlib.h> ...

  10. 芯片SIAT-002测试PCB板设计

    这个板子,从原理图到PCB板,总共画了6天,接近一个星期!虽然说各种麻烦,但总算学到了一些新知识.谨记以备后查. 附注: 模拟地与数字地详解 单片机晶振电路 1. 走线规划 针对采用BGA封装及引脚数 ...