• vue.js路由如何配置,及全局前置路由守卫(做所有请求登录验证)、路由独享守卫(访问路由前身份验证)


    1.编写路由配置文件 router.js

    以及配置全局前置路由守卫和路由独享守卫
    1. //路由配置文件
    2. //作用是将指定的路由地址切换成对应的模块
    3. // eslint-disable-next-line no-unused-vars
    4. import Router from "vue-router"
    5. // eslint-disable-next-line no-unused-vars
    6. import Vue from "vue"
    7. //在Vue中加载路由模块
    8. Vue.use(Router)
    9. // eslint-disable-next-line no-unused-vars
    10. // 定义 Foo 组件,你可以在 Vue 应用中的任何地方使用它,
    11. // 可以在路由配置中将 Foo 作为一个路由组件来使用,
    12. // 或者在其他组件中通过标签的形式来嵌套使用它。
    13. // const Foo = { template: '
      foo
      ' }
    14. // 写路由表
    15. const routes = [
    16. // 进入vue项目默认进入登录页面
    17. {
    18. path: "/",
    19. redirect: "/Login"
    20. },
    21. {
    22. path: "/Login",
    23. component: () => import("../view/Login"),
    24. meta: {
    25. skipAuthCheck: true // 添加一个标记,表示不需要进行身份验证检查
    26. }
    27. },
    28. {
    29. path: "/index",
    30. component: () => import("../components/index"),
    31. children: [
    32. // 默认显示select_update_delete页面
    33. { path: '/', redirect: "/hello" },
    34. {
    35. path: "/hello",
    36. component: () => import("../components/hello"),
    37. },
    38. //用户管理
    39. {
    40. path: '/s_u_d', component: () => import("../components/system/UserManger/select_update_delete"),
    41. meta: { requiresAuth: true },
    42. beforeEnter: user_typeValidate('系统管理员'),
    43. },
    44. {
    45. path: '/addUser', component: () => import("../components/system/UserManger/addUserForm"),
    46. meta: { requiresAuth: true },
    47. beforeEnter: user_typeValidate('系统管理员'),
    48. },
    49. //出诊管理
    50. {
    51. path: '/chuZhen', component: () => import("../components/system/MenZhenManger/chuZhenManger"),
    52. meta: { requiresAuth: true },
    53. beforeEnter: user_typeValidate('系统管理员'),
    54. },
    55. {
    56. path: '/addChuZhen', component: () => import("../components/system/MenZhenManger/addChuZhenForm"),
    57. meta: { requiresAuth: true },
    58. beforeEnter: user_typeValidate('系统管理员'),
    59. },
    60. //就诊管理
    61. {
    62. path: '/jiuZhen', component: () => import("../components/medical/jiuZhenManger/jiuZhenManger"),
    63. meta: { requiresAuth: true },
    64. beforeEnter: user_typeValidate('门诊医生'),
    65. },
    66. {
    67. path: '/advice', component: () => import("../components/medical/jiuZhenManger/addDoctorAdvice"),
    68. meta: { requiresAuth: true },
    69. beforeEnter: user_typeValidate('门诊医生'),
    70. },
    71. {
    72. path: '/view', component: () => import("../components/medical/jiuZhenManger/viewJiuZhen"),
    73. meta: { requiresAuth: true },
    74. beforeEnter: user_typeValidate('门诊医生'),
    75. },
    76. ],
    77. },
    78. ]
    79. const routers = new Router({
    80. routes
    81. });
    82. // 全局前置守卫
    83. // to当前即将要进入的路由对象
    84. routers.beforeEach((to, from, next) => {
    85. //如果当前的访问的请求是Login放行
    86. if (to.path === '/Login') {
    87. console.log(to);
    88. console.log(to.path);
    89. next();
    90. }
    91. else {
    92. //其余访问请求判断用户是否登录
    93. if (!isLoggedIn()) {
    94. console.log(to);
    95. console.log(to.meta);
    96. console.log("抱歉你未登录");
    97. next({ path: '/Login' }); // 如果用户未登录,则重定向到登录页面
    98. } else {
    99. next();
    100. }
    101. }
    102. })
    103. //登录验证函数
    104. function isLoggedIn() {
    105. console.log("进入路由守卫");
    106. // 在这里实现检查用户是否已登录的逻辑,例如检查是否有有效的令牌或会话
    107. // 如果已登录,返回true,否则返回false
    108. const user = sessionStorage.getItem('user'); // 从sessionStorage中获取会话信息
    109. return user !== null; // 如果会话信息存在,则用户已登录
    110. }
    111. // 用户类型身份验证 路由独享守卫函数
    112. function user_typeValidate(requiredRole) {
    113. return (to, from, next) => {
    114. // 在进入该路由之前执行的逻辑,检查用户是否具有所需的用户类型
    115. if (localStorage.getItem("user_type") === requiredRole) {
    116. next(); // 放行,允许进入
    117. } else {
    118. next(false); // 表示阻止用户导航,保持在当前路由
    119. }
    120. };
    121. }
    122. // 到处vue路由表
    123. export default routers;
    124. // 防止连续点击多次路由报错
    125. let routerPush = Router.prototype.push;
    126. let routerReplace = Router.prototype.replace;
    127. // push
    128. Router.prototype.push = function push(location) {
    129. return routerPush.call(this, location).catch(err => err)
    130. }
    131. // replace
    132. Router.prototype.replace = function push(location) {
    133. return routerReplace.call(this, location).catch(err => err)
    134. }

    其中函数 也可以写在文件里面 比如user_typeValidate(requiredRole)身份验证函数

    步骤如下:

    首先,创建一个名为 authGuard.js 的文件,用于定义可重用的路由独享守卫身份验证函数。如下

    1. export function user_typeValidate(requiredRole) {
    2.     return (to, from, next) => {
    3.         // 在进入该路由之前执行的逻辑,检查用户是否具有所需的用户类型
    4.         if (localStorage.getItem("user_type") === requiredRole) {
    5.             next(); // 放行,允许进入
    6.         } else {
    7.             next(false); // 表示阻止用户导航,保持在当前路由
    8.         }
    9.     };
    10. }

    然后,在写路由表之前导入这个函数。例如:

    1. import { user_typeValidate } from './authGuard.js'; // 导入守卫函数
    2. const routes = [
    3. {
    4. path: "/index",
    5. component: () => import("../components/index"),
    6. children: [
    7. // 默认显示select_update_delete页面
    8. { path: '/', redirect: "/hello" },
    9. {
    10. path: "/hello",
    11. component: () => import("../components/hello"),
    12. },
    13. // 用户管理
    14. {
    15. path: "/s_u_d",
    16. component: () => import("../components/system/UserManger/select_update_delete"),
    17. meta: { requiresAuth: true },
    18. beforeEnter: user_typeValidate('系统管理员'), // 使用可重用的守卫函数
    19. },
    20. // ...其他路由配置
    21. ],
    22. },
    23. ];
    24. export default routes;

    在文件加入设置解决路由多次点击报错  

    1. // 防止连续点击多次路由报错
    2. let routerPush = Router.prototype.push;
    3. let routerReplace = Router.prototype.replace;
    4. // push
    5. Router.prototype.push = function push(location) {
    6. return routerPush.call(this, location).catch(err => err)
    7. }
    8. // replace
    9. Router.prototype.replace = function push(location) {
    10. return routerReplace.call(this, location).catch(err => err)
    11. }

    在main.js引入

    1. import Vue from 'vue'
    2. import App from './App.vue'
    3. import router from "@/router/router"
    4. import element from 'element-ui';
    5. import axios from 'axios'
    6. import 'element-ui/lib/theme-chalk/index.css';
    7. Vue.config.productionTip = false
    8. // vue的原型对象
    9. Vue.prototype.axios = axios
    10. Vue.use(element)
    11. new Vue({
    12. router,
    13. render: h => h(App),
    14. }).$mount('#app')

  • 相关阅读:
    ClickHouse进阶(七):Clickhouse数据查询-1
    使用python读写xlsx格式中的数据【xlrd、pywin32】
    不同类型跨链桥中可能存在的安全隐患
    Pycharm 2023 设置远程调试
    Go入门系列:你好,世界
    【web前端特效源码】使用HTML5& CSS&Js实现倒计时数字
    String类
    接口自动化测试之 —— requests模块详解!
    在线客服系统统计员工的一些工作量,有哪些统计维度?
    c# 控制台应用程序
  • 原文地址:https://blog.csdn.net/qq_58647634/article/details/132897907