当前位置:网站首页>Global, exclusive and local routing guard
Global, exclusive and local routing guard
2022-04-23 02:23:00 【Four seasons milk green sugar@】
One 、 Create route
// Create a router , And exposed
export default new VueRouter({
routes: [{
path: '/about',
component: About
},
{
path: '/home',
component: Home
},
]
})
Two 、 Global route guard
Global guards are set in router above , This can be router/index.js Found inside . You can set up a global guard here , Of course, you can also set up global guards in other places , such as main.js perhaps App.vue. It depends on the specific needs .
Route guard : Protect the security of routing .( jurisdiction )
(1) Global front route guard
Before each route switching / When initializing - Called
Front guard of the whole situation beforeEach The callback function of the global guard accepts three parameters as :to,from,next
Three parameter descriptions :
- to Jump into the target routing object
- from The object is ready to leave the current route
- next After calling this method , Go to the next hook function , Function like express perhaps koa Called in the middleware next
next Function description :
- If next Function normal call next() Indicates normal entry into the jump route , The navigation status is confirmed
- If next When the function is called, it is passed in false, Such as :next(false), Indicates terminal navigation jump
- next Functions can also pass parameters , Indicates a jump to an instruction route next("/"), or next({ path:"/" }) Parameter routing
router.beforeEach((to, from, next) => {
console.log(' Front routing guard ', to, from);
if (to.meta.isAuth) { // Whether authentication is needed
if (localStorage.getItem('school') === 'atguigu') {
next() // release
} else {
alert(' The school name is wrong , No authority !')
}
} else {
next()
}
});
(2) Global post route guard
afterEach Global route guard
afterEach The callback function of the global guard accepts two parameters: to,from
Description of two parameters :
- to Jump into the target routing object
- from The object is ready to leave the current route
afterEach No, next() Callback function , Because in afterEach When called , The route has been skipped .
router.afterEach((to, from) => {
console.log(' Post routing guard ', to, from);
document.title = to.meta.title || ' Silicon Valley system '
});
3、 ... and 、 Exclusive routing guard
The hook function used is consistent with the global routing guard , by beforeEnter, The difference is , The route exclusive guard is defined in the route record , The global routing guard is defined in the entry file , The route exclusive guard is only valid when the route enters , The global route guard is that all route jumps will be blocked .
It refers to the hook function that can also be set when configuring a single route .
// Are you authorized to
meta: { isAuth: true, title: ' Journalism ' },
// Exclusive routing guard
beforeEnter: (to, from, next) => {
// ...
console.log(' Exclusive routing guard ', to, from);
if (to.meta.isAuth) { // Whether authentication is needed
if (localStorage.getItem('school') === 'atguigu') {
next() // release
} else {
alert(' The school name is wrong , No authority !')
}
} else {
next()
}
}
Four 、 Intra group routing guard
1. Guards within a component are defined inside the component , Routing guard in component options object
2. There are three routing guards inside the component , by :beforeRouteEnter,beforeRouteUpdate,beforeRouteLeave.
3. beforeRouteEnter Top note before component creation , Component instances cannot be used this
4. beforeRouteUpdate Route changed , But when a component is reused , such as : Dynamic routing
5. beforeRouteLeave Called by this component when the navigation leaves
export default {
name:'About',
// Through routing rules , Entering the component is called
beforeRouteEnter (to, from, next) {
console.log('About-beforeRouteEnter', to, from);
if (to.meta.isAuth) { // Whether authentication is needed
if (localStorage.getItem('school') === 'atguigu') {
next() // release
} else {
alert(' The school name is wrong , No authority !')
}
} else {
next()
}
},
// Through routing rules , Called when leaving the component
beforeRouteLeave (to, from, next) {
console.log('About-beforeRouteLeave', to, from);
next()
}
}

版权声明
本文为[Four seasons milk green sugar@]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230221596951.html
边栏推荐
- A simple and open source navigation website source code
- Arduino esp8266 network upgrade OTA
- PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]
- Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23
- 【2019-CVPR-3D人体姿态估计】Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views
- C语言中*与&的用法与区别 以及关键字static和volatile 的含义
- 013_基于Session实现短信验证码登录流程分析
- App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
- How to recognize products from the perspective of Dialectics
- Today will finally write system out. Println()
猜你喜欢

009_Redis_RedisTemplate入门

Day18 -- stack queue

Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space

006_ redis_ Sortedset type

010_StringRedisTemplate

Latin goat (20204-2022) - daily question 1

都是做全屋智能的,Aqara和HomeKit到底有什么不同?

89 logistic回歸用戶畫像用戶響應度預測

用TensorFlow实现线性回归(包括过程中出现的问题及解决方法)

Redis memory recycling strategy
随机推荐
小程序 canvas 画布半圆环
89 logistic regression user portrait user response prediction
Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
[assembly language] understand "stack" from the lowest point of view
Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield
关于局域网浅谈
用TensorFlow实现线性回归(包括过程中出现的问题及解决方法)
The 16th day of sprint to the big factory, noip popularization Group Three Kingdoms game
002_Redis_String类型常见的操作命令
想用Mac学习sql,主要给自己个充足理由买Mac听听意见
Usage of vector common interface
Redis memory recycling strategy
LeetCode 349. Intersection of two arrays (simple, array) Day12
leetcode:27. Remove element [count remove]
都是做全屋智能的,Aqara和HomeKit到底有什么不同?
手写内存池以及原理代码分析【C语言】
Common formatting problems after word writing
PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]
类初始化和实例初始化面试题
VMware virtual machine installation openwrt as side route single arm route img image to vmdk