首页 > 其他分享 >Vue学习笔记70--全局前置-路由守卫 + 后置路由守卫 + 独享守卫 + 组件内守卫

Vue学习笔记70--全局前置-路由守卫 + 后置路由守卫 + 独享守卫 + 组件内守卫

时间:2024-04-02 11:56:13浏览次数:18  
标签:Vue name title 守卫 meta props id 路由

路由守卫简介

  1. 作用:用于对路由进行权限控制
  2. 分类:全局守卫(前置路由守卫 + 后置路由守卫)、独享守卫、组件内守卫

全局--前置路由守卫 + 后置守卫 

示例

  1 import Vue from 'vue'
  2 import VueRouter from 'vue-router'
  3 import Home from '../views/Home.vue'
  4 import About from '../views/About.vue'
  5 import News from '@/views/home/News.vue'
  6 import Message from '@/views/home/Message.vue'
  7 import Detail from '@/views/home/Detail.vue'
  8 
  9 //Vue中使用router插件
 10 Vue.use(VueRouter)
 11 
 12 // 新建路由实例
 13 //路由配置,配置路由路径与组件的对应关系
 14 const router = new VueRouter({
 15   routes: [
 16     {
 17       path: '/home',
 18       name: 'Home',
 19       meta: { title: '首页' },
 20       // 路由元数据
 21       component: () => import('../views/Home.vue'),
 22       children: [
 23         {
 24         // 路由元数据
 25        meta: { isAuth: true, title: '新闻' },
 26           path: '/home/news',//path最左侧/代表根路径
 27           name: 'News',
 28           component: News
 29         },
 30         {
 31           // 路由元数据
 32           meta: { isAuth: true, title: '消息' },
 33           path: 'Message',//这里没有用/ 表示相对路径
 34           component: Message,
 35           name: 'Message',
 36           children: [
 37             {
 38               name: 'detail',
 39               meta: { title: '详情' },
 40               path: '/home/detail/:id/:title',//path最左侧/代表根路径
 41               component: Detail,
 42 
 43               // props写法一:值为对象,该对象中的所有key-value都会以props的形式传给Detail组件
 44               // props: {a:1,b:'你好'}  //不常用,传固定值
 45 
 46               // props写法二:值为bool,若值为真--就会把该路由组件收到的说有params(不能接受到query参数)参数以props的形式传给Detail组件
 47               // props: true
 48 
 49               // props写法三:值为函数
 50               props ($route) {
 51                 // return { id: $route.query.id, title: $route.query.title }
 52                 return { id: $route.params.id, title: $route.params.title }
 53               }
 54 
 55               // props ({ query }) {  //结构赋值
 56               // props ({ params }) {
 57               //   // return { id: $route.query.id, title: $route.query.title }
 58               //   return { id: params.id, title: params.title }
 59               // }
 60 
 61               // 结构赋值连续写法
 62               // props ({ params: { id, title } }) {
 63               //   // return { id: $route.query.id, title: $route.query.title }
 64               //   return { id, title }
 65               // }
 66 
 67             },
 68           ]
 69         },
 70         {
 71           // 路由元数据
 72           meta: { isAuth: true, title: '新闻' },
 73           path: '',
 74           name: 'News',
 75           redirect: '/home/News'
 76         }
 77       ]
 78     },
 79     {
 80       path: '/about',
 81       name: 'About',
 82       meta: { title: '关于' },
 83       component: () => import('../views/About.vue'),
 84     },
 85     {//根路由--默认
 86       path: '/',
 87       name: 'Home',
 88       meta: { title: '首页' },
 89       redirect: '/home'
 90     }
 91   ]
 92 })
 93 
 94 /* 
 95 全局(前置路由守卫)
 96 初始化时被调用 + 每次路由切换之前
 97  */
 98 router.beforeEach((to, from, next) => {
 99   console.log('全局前置路由守卫 to name==>', to, from)
100 
101   if (to.meta.isAuth) {//判断是否需要鉴权
102     // if (to.name === 'Home' || to.name === 'About' || to.name === 'News') {
103     if (to.name === 'News') {
104       // document.title = to.meta.title || '欢迎来到首页'
105       next()//放行
106     }
107     else {
108       // document.title = to.meta.title || '欢迎来到首页'
109       alert('你没有操作权限')
110     }
111   }
112 })
113 
114 /* 
115 全局后置路由守卫
116 初始化的时候被调用,每次路由切换之后调用
117  */
118 router.afterEach((to, from) => {
119   console.log('全局后置路由守卫 to name==>', to, from)
120   document.title = to.meta.title || '欢迎来到首页'
121 })
122 
123 //导出路由实例,在main.js中导入使用
124 export default router  

独享守卫

注:独享路由守卫,可以和全局路由守卫(前置、后置)组合使用,需结合实际情况自行处理......

 

示例:

  1 import Vue from 'vue'
  2 import VueRouter from 'vue-router'
  3 import Home from '../views/Home.vue'
  4 import About from '../views/About.vue'
  5 import News from '@/views/home/News.vue'
  6 import Message from '@/views/home/Message.vue'
  7 import Detail from '@/views/home/Detail.vue'
  8 
  9 //Vue中使用router插件
 10 Vue.use(VueRouter)
 11 
 12 // 新建路由实例
 13 //路由配置,配置路由路径与组件的对应关系
 14 const router = new VueRouter({
 15   routes: [
 16     {
 17       path: '/home',
 18       name: 'Home',
 19       meta: { title: '首页' },
 20       // 路由元数据
 21       component: () => import('../views/Home.vue'),
 22       children: [
 23         {
 24           // 路由元数据
 25           meta: { isAuth: true, title: '新闻' },
 26           path: '/home/news',//path最左侧/代表根路径
 27           name: 'News',
 28           component: News
 29         },
 30         {
 31           // 路由元数据
 32           meta: { isAuth: true, title: '消息' },
 33           path: 'Message',//这里没有用/ 表示相对路径
 34           component: Message,
 35           name: 'Message',
 36           children: [
 37             {
 38               name: 'detail',
 39               meta: { title: '详情' },
 40               path: '/home/detail/:id/:title',//path最左侧/代表根路径
 41               component: Detail,
 42 
 43               // props写法一:值为对象,该对象中的所有key-value都会以props的形式传给Detail组件
 44               // props: {a:1,b:'你好'}  //不常用,传固定值
 45 
 46               // props写法二:值为bool,若值为真--就会把该路由组件收到的说有params(不能接受到query参数)参数以props的形式传给Detail组件
 47               // props: true
 48 
 49               // props写法三:值为函数
 50               props ($route) {
 51                 // return { id: $route.query.id, title: $route.query.title }
 52                 return { id: $route.params.id, title: $route.params.title }
 53               }
 54 
 55               // props ({ query }) {  //结构赋值
 56               // props ({ params }) {
 57               //   // return { id: $route.query.id, title: $route.query.title }
 58               //   return { id: params.id, title: params.title }
 59               // }
 60 
 61               // 结构赋值连续写法
 62               // props ({ params: { id, title } }) {
 63               //   // return { id: $route.query.id, title: $route.query.title }
 64               //   return { id, title }
 65               // }
 66 
 67             },
 68           ]
 69         },
 70         {
 71           // 路由元数据
 72           meta: { isAuth: true, title: '新闻' },
 73           path: '',
 74           name: 'News',
 75           redirect: '/home/News'
 76         }
 77       ]
 78     },
 79     {
 80       path: '/about',
 81       name: 'About',
 82       meta: { title: '关于' },
 83       component: () => import('../views/About.vue'),
 84 
 85       // 独享路由守卫
 86       beforeEnter: ((to, from, next) => {
 87         console.log('独享路由守卫 to name==>', to, from)
 88         if (to.meta.isAuth) {//判断是否需要鉴权
 89           if (to.name === 'About') {
 90             next()//放行
 91           }
 92           else {
 93             alert('你没有操作权限')
 94           }
 95         }
 96       }),
 97     },
 98     {//根路由--默认
 99       path: '/',
100       name: 'Home',
101       meta: { title: '首页' },
102       redirect: '/home'
103     }
104   ]
105 })
106 
107 /* 
108 全局后置路由守卫
109 初始化的时候被调用,每次路由切换之后调用
110  */
111 router.afterEach((to, from) => {
112   console.log('全局后置路由守卫 to name==>', to, from)
113   document.title = to.meta.title || '欢迎来到首页'
114 })
115 //导出路由实例,在main.js中导入使用
116 export default router  

组件内守卫

 

标签:Vue,name,title,守卫,meta,props,id,路由
From: https://www.cnblogs.com/YYkun/p/18108546

相关文章

  • vue中表单修改提交前利用watch找出新数据和原来数据之间的改动
    <template><div><[email protected]="submitForm"><inputv-model="formData.name"type="text"placeholder="Name"><inputv-model="formData.email"type......
  • Ant Design Vue中的table与pagination的联合使用
    效果: 代码:<a-table:dataSource="dataSource":columns="columns":pagination="pagination"@change="handleTableChange":scroll="{x:'100%',y:600}"></a-table>......
  • vue3从精通到入门9:计算属性computed
    在Vue3中,computed 是一个用于创建计算属性的工具,它基于组件的响应式依赖进行复杂的计算,并返回一个新的响应式引用。计算属性是Vue的一个核心概念,它提供了一种声明式的方式来执行基于其依赖的响应式数据的计算。computed使用:计算属性与常规属性类似,但是它们是基于它们......
  • 2024前端vue面试问题以及答案
    Vuex相关问题Vuex是什么,它解决了什么问题?Vuex是一个专为Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。Vuex的核心概念有哪些?State:存储所有组件的状态。Getters:类似于计算属......
  • VLAN间路由
            部署了VLAN的传统交换机不能实现不同VLAN间的二层报文转发,因此必须引入路由技术来实现不同VLAN间的通信。VLAN路由可以通过二层交换机配合路由器来实现,也可以通过三层交换机来实现;VLAN间通讯限制        每个VLAN都是一个独立的广播域,不同的VLAN之......
  • Vue 3.0 + Element-Plus + Ruoyi
    1项目搭建1.1项目源码1.2项目架构后端目录结构ruoyi-admin:后台服务的核心模块,包含主要的业务处理逻辑。ruoyi-common:公共模块,包含工具类和通用代码。ruoyi-framework:框架核心,包含安全、配置和核心管理功能。ruoyi-generator:代码生成模块,用于自动生成代码。ruoyi-quar......
  • Vue.js基础指令
    (在讲指令之前,可以先了解插值表达式,如果已经知道,当我没说)一.插值表达式1.数据绑定最常见的形式就是双大括号的文本插值,Mustache上属性的值替代。只要绑定的数据对象上属性发生了改变,插值处的内容都会更新。,message是将数据解析成纯文本的,也就是说,就算中含有了html,message......
  • vue3 设置el-dialog height超过滚动条
     方法一:<stylescoped>::v-deep.el-dialog.el-dialog-body{height:500px;overflow-y:auto;}</style> 如果要设置动态的高度话,则要在setup里面设置 <script>exportdefaultdefineComponent({setup:{constcssContent=ref({heigh......
  • 前端技术栈和Vue学习总结
    前端技术栈+Vue笔记ES6新特性1.let1)let声明有严格的局部作用域​ 此时"console.log("job="+job)"将报错 {varname="zy学习";letjob="java工程师";console.log("name="+name)console.log("job=&quo......
  • vue在组件销毁的时候将异步请求撤销
    背景Vue2+ArcGISJS加载图层使用FeatureLayer.queryFeatures(query)对服务端执行地理数据查询请求。该请求为fetch类型。复现Bug:刚打开专题A,未等加载完,点击快速切换到共用同一个地图的新专题B,地图却加载专题A的数据图层。原因:由于地图还是用的同一个对象,且该函数Feat......