網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
全局后置路由守衛(wèi)(afterEach)
- 功能:每一次切換任意路由組件之后都會(huì)被調(diào)用,相當(dāng)于在進(jìn)入下一個(gè)路由組件之后設(shè)置一個(gè)權(quán)限。
使用原理
- 代碼創(chuàng)建的位置:
- 在創(chuàng)建router之后(
const router = new VueRouter
) - 暴露router之前(
export default router
)
- 在創(chuàng)建router之后(
- afterEach中的回調(diào)函數(shù)在什么時(shí)候被調(diào)用?
- 在初始化的時(shí)候執(zhí)行一次,以后每一次切換完任意一個(gè)路由組件之后被調(diào)用
- 回調(diào)函數(shù)沒有固定形式,普通函數(shù)或箭頭函數(shù)都可以
- 回調(diào)函數(shù)有兩個(gè)參數(shù)(沒有next函數(shù)):
- to參數(shù):to是一個(gè)路由對(duì)象,表示到哪兒去(跳轉(zhuǎn)的下一個(gè)路由組件)。
- from參數(shù):form是一個(gè)路由對(duì)象,表示從哪來(lái)(從哪個(gè)路由切換過(guò)來(lái)的)。
- 格式:
router.afterEach((to, from) => {})
- 自定義屬性(meta):在路由對(duì)象的添加meta(路由元)
- 格式:
meta : {屬性名:''}
- title屬性:可以修改頁(yè)面標(biāo)題
- 格式:
import VueRouter from 'vue-router'
import HuBei from '../pages/HuBei'
import City from '../pages/City'
const router = new VueRouter({
routes : [
{
name : 'bei',
path : '/hubei',
component : HuBei,
meta : {title : '湖北省'},
children : [
{
name : 'wh',
path : 'wuhan',
component : City,
props : true,
meta : {
isAuth : true,
title : '武漢市'
}
},
{
name : 'hs',
path : 'huangshi',
component : City,
props : true,
meta : {
isAuth : true,
title : '黃石市'
}
}
]
}
]
})
// 全局后置路由守衛(wèi)
router.afterEach((to, from) => {
document.title = to.meta.title || '歡迎使用'
})
export default router
// App.vue
<template>
<div>
<h2>省</h2>
<div>
<ul>
<li>
<router-link to="/hubei">湖北省</router-link>
</li>
</ul>
<router-view></router-view>
</div>
</div>
</template>
<script>
export default {
name : 'App'
}
</script>
// HuBei.vue
<template>
<div>
<h2>市</h2>
<div>
<ul>
<li>
<router-link :to="{
name : 'wh',
params : {
a1 : wh[0],
a2 : wh[1],
a3 : wh[2],
}
}">
武漢市
</router-link>
</li>
<li>
<router-link :to="{
name : 'hs',
params : {
a1 : hs[0],
a2 : hs[1],
a3 : hs[2],
}
}">
黃石市
</router-link>
</li>
</ul>
<router-view></router-view>
</div>
</div>
</template>
<script>
export default {
name : 'HuBei',
data(){
return{
wh : ['江岸區(qū)', '江漢區(qū)', '橋口區(qū)'],
hs : ['下陸區(qū)', '鐵山區(qū)', '西塞山區(qū)']
}
}
}
</script>
// City.vue
<template>
<div>
<h2>區(qū)</h2>
<ul>
<li>{{a1}}</li>
<li>{{a2}}</li>
<li>{{a3}}</li>
</ul>
</div>
</template>
<script>
export default {
name : 'City',
props : ['a1', 'a2', 'a3']
}
</script>
原文鏈接:https://blog.csdn.net/weixin_47957908/article/details/134329504
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-11-04 C++淺析內(nèi)存分區(qū)模型概念與示例_C 語(yǔ)言
- 2023-04-11 Python中字符串類型代碼的執(zhí)行函數(shù)——eval()、exec()和compile()詳解_pyt
- 2023-05-22 Redis數(shù)據(jù)結(jié)構(gòu)類型示例解析_Redis
- 2022-04-25 .NET避免裝箱的方法_實(shí)用技巧
- 2022-10-04 golang中time包之時(shí)間間隔格式化和秒、毫秒、納秒等時(shí)間戳格式輸出的方法實(shí)例_Golang
- 2023-02-01 Python?asyncore?socket客戶端開發(fā)基本使用教程_python
- 2022-03-14 跨域:Response to preflight request doesn t pass acce
- 2022-03-22 Linux系統(tǒng)下gcc命令使用詳解_Linux
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支