当前位置:网站首页>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
边栏推荐
- PTA: 点赞狂魔
- leetcode:27. 移除元素【count remove小操作】
- 想用Mac学习sql,主要给自己个充足理由买Mac听听意见
- 011_RedisTemplate操作Hash
- Leetcode39 combined sum
- wordpress 调用指定页面内容详解2 get_children()
- Leetcode40 - total number of combinations II
- Latin goat (20204-2022) - daily question 1
- Want to experience homekit smart home? Why don't you take a look at this smart ecosystem
- How does Axure set the content of the text box to the current date when the page is loaded
猜你喜欢

Leetcode46 Full Permutation
![App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]](/img/86/77b67fd28d2583e12f397430a9a62a.png)
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
![PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]](/img/ae/a6681df6c3992c7fd588334623901c.png)
PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]

day18--栈队列

Talk about current limiting

How does Axure set the content of the text box to the current date when the page is loaded

006_redis_SortedSet类型

【汇编语言】从最底层的角度理解“堆栈”

Redis memory recycling strategy

【2019-CVPR-3D人体姿态估计】Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views
随机推荐
arduino esp8266 网络升级 OTA
Latin goat (20204-2022) - daily question 1
Dynamic memory management
tp6阿裏雲短信 window 報 cURL error 60: SSL certificate problem: unable to get local issuer certificate
How many steps are there from open source enthusiasts to Apache directors?
电源电路设计原来是这么回事
Summary of I / O knowledge points
009_ Redis_ Getting started with redistemplate
012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
PTA: 点赞狂魔
easyswoole环境配置
006_redis_jedis快速入门
005_ redis_ Set set
day18--栈队列
Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
Flink real-time data warehouse project - Design and implementation of DWS layer
Dynamic batch processing and static batch processing of unity
C standard library - < time h>
【Chrome扩展程序】content_script的跨域问题
Unity editor hierarchy drop-down menu extension