当前位置:网站首页>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
边栏推荐
- Kubernetes cluster installation based on Kirin SP10 server version
- Applet reads files
- 配置iptables实现本地端口转发的方法详解
- Tp6 Alibaba cloud SMS window reports curl error 60: SSL certificate problem: unable to get local issuer certificate
- 010_StringRedisTemplate
- PHP sorting of interview questions on April 20, 2022
- Time. In ANSI standard library H header file
- Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
- Introduction to micro build low code zero Foundation (lesson 2)
- 89 logistic回歸用戶畫像用戶響應度預測
猜你喜欢
Why is one plus one equal to two
002_ Redis_ Common operation commands of string type
If 404 page is like this | daily anecdotes
010_StringRedisTemplate
013_ Analysis of SMS verification code login process based on session
leetcode:27. 移除元素【count remove小操作】
Real math problems in 1958 college entrance examination
PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]
Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23
【汇编语言】从最底层的角度理解“堆栈”
随机推荐
Halo open source project learning (I): project launch
从开源爱好者到 Apache 董事,一共分几步?
Time. In ANSI standard library H header file
arduino esp8266 网络升级 OTA
Real math problems in 1958 college entrance examination
配置iptables实现本地端口转发的方法详解
LeetCode 283. Move zero (simple, array) Day12
RT_ Thread ask and answer
手写内存池以及原理代码分析【C语言】
A simple and open source navigation website source code
005_ redis_ Set set
智能辅助功能丰富,思皓X6安全配置曝光:将于4月23日预售
LeetCode 349. Intersection of two arrays (simple, array) Day12
类初始化和实例初始化面试题
不断下沉的咖啡业,是虚假的繁荣还是破局的前夜?
009_ Redis_ Getting started with redistemplate
The 16th day of sprint to the big factory, noip popularization Group Three Kingdoms game
89 logistic回归用户画像用户响应度预测
Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield
Why is one plus one equal to two