VueJS - Vue Router
Vue Router
Vue-router理解
- 理解:
vue的一個插件庫,專門用來實現SPA應用
- 對SPA應用的理解:
- 單頁Web應用 (single page web application)
- 整個應用只有一個完整的頁面
- 點擊頁面中的導航鏈接不會刷新頁面,只會做頁面的局部更新
- 數據需要用過ajax請求獲取
route路由的理解
- 理解:
- 一個就是一組映射關係 (key - value)
key為路徑,value可能是function或component
- 路由的分類:
- 後端路由:
- 理解:
value是function,統御處理客戶端提交的請求 - 工作過程:服務器接收到一個請求時,根據請求路徑找到匹配的函數來處理請求,返回響應函數
- 理解:
- 前端路由:
- 理解:
value是component,用於展示頁面內容 - 工作過程:當瀏覽器的路徑改變時,對應的組件就會顯示
- 理解:
- 後端路由:
Vue-router的基本使用
- 安裝
1
npm i vue-router
- 在
main.js中應用插件:1
2
3
4// 引入
import VueRouter from 'vue-router'
// 應用
Vue.use(VueRouter)
- 編寫
router配置項:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19// 引入VueRouter
import VueRouter from 'vue-router'
// 引入組件
import About from '../components/About'
import Home from '../components/Home'
// 創建並暴露router實例對象,去管理路由規則
export default new VueRouter({
routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home
}
]
})
- 實現切換:借助
<router-link>標籤實現路由的切換active-class可配置高亮樣式1
2<router-link active-class="active" to="/about">About</router-link>
<router-link active-class="active" to="/home">Home</router-link>
- 指定展示位置:借助
<router-view>指定組件的呈現位置1
<router-view></router-view>
- 注意:
- 路由組件通常存放在
pages文件夾,一般組件通常存放在components文件夾 - 通過切換,「隱藏」了的路由組件,默認是被銷毀掉的,需要的時候再去掛載
- 每個組件都有自己的
$route屬性,裏面存放著自己的路由信息 - 整個應用只有一個router,可以通過組件的
$router屬性獲取
- 路由組件通常存放在
嵌套路由(多級路由)
配置路由規則時,使用
children配置項:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home,
children:[
{
path:'news', // 此處一定不要寫'/news'
component:News
},
{
path:'message', // 此處一定不要寫'/message'
component:Message
}
]
}
]跳轉(要寫完整路徑):
1
<router-link to="/home/news">News</router-link>
路由的query參數
- 傳遞參數
1
2
3
4
5
6
7
8
9
10
11
12
13<!-- 跳轉路由並攜帶query參數,to的字符串寫法 -->
<router-link :to="`/home/message/detail?id=${m.id}&title=${m.title}`">{{m.title}}</router-link>
<!-- 跳轉路由並攜帶query參數,to的對象寫法 -->
<router-link
:to="{
path:'home/message/detail',
query:{
id:m.id,
title:m.title
}
}"
>{{m.title}}</router-link>
- 接收參數:
1
2$route.query.id
$route.query.title
命名路由
- 作用:可以簡化路由的跳轉(路由地址)
使用方法:
給路由取名字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17{
path:'/demo',
component:Demo,
children:[
{
path:'test',
component:Test,
children:[
{
name:'hello', // 給路由命名
path:'welcome',
component:Hello,
}
]
}
]
}簡化跳轉
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<!-- 簡化前寫法:完整路徑 -->
<router-link to="/demo/test/welcome"></router-link>
<!-- 簡化後寫法:直接通過名字跳轉 -->
<router-link :to="{name:'hello'}"></router-link>
<!-- 簡化後配合傳遞參數 -->
<router-link
:to="{
name:'hello',
query:{
id:xxx,
title:yyy
}
}"
></router-link>
路由的params參數
- 配置路由,聲明接收的params參數:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17{
path:'/home',
component:Home,
children:[
{
path:'message',
component:Message,
children:[
{
name:'detailRouter',
path:'detail/:id/:title', // 使用佔位聲明接收params參數
component:Detail,
}
]
}
]
}
- 傳遞參數:
1
2
3
4
5
6
7
8
9
10
11
12
13<!-- 跳轉路由並攜帶params參數,to的字符串寫法 -->
<router-link :to="`/home/message/detail/${m.id}/${m.title}`">{{m.title}}</router-link>
<!-- 跳轉路由並攜帶params參數,to的對象寫法 -->
<router-link
:to="{
name:'detailRouter',
params:{
id:m.id,
title:m.title
}
}"
>{{m.title}}</router-link>- 注意點:路由攜帶params參數時,若使用to的對象寫法,則不能使用
path配置項,必須使用name配置
- 注意點:路由攜帶params參數時,若使用to的對象寫法,則不能使用
- 接收參數
1
2$route.params.id
$route.params.title
路由的props配置
- 作用:讓路由器組件更方便的接收到參數
- 使用方法:
1
2
3
4
5
6
7
8
9
10
11
12
13// 第一種寫法:對象。該對象中的所欲key-value都會以props的形式傳給Detail組件
props:{a:1,b:'hello'}
// 第二種寫法:值為布爾值。若布爾值為真,則把該路由組件收到的所有params參數,以props的形式傳給Detail組件
props:true
// 第三種寫法:值為函數。該函數返回的對象中每一組key-value都會通過props傳給Detail組件
props($route){
return{
id:$route.query.id,
title:$route.query.title
}
}
replace屬性
- 作用:控制路由器跳轉時操作瀏覽器歷史紀錄的模式
- 瀏覽器的歷史紀錄有兩種寫入方式:分別是
push和replacepush是追加歷史紀錄replace是替換當前紀錄- 路由跳轉時默認為
push
- 開啟
replace模式:1
<router-link replace ...></router-link>
編程式路由導航
- 作用:不借助
<router-link>實現路由跳轉,讓路由跳轉更加靈活
- 具體代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21// 以 push模式 跳轉到指定路由
this.$router.push({
name:'routeName',
params:{
id:xxx,
title:xxx
}
})
// 以 replace模式 跳轉到指定路由
this.$router.replace({
name:'routeName',
params:{
id:xxx,
title:xxx
}
})
this.$router.forward() // 前進
this.$router.back() // 後退
this.$router.go(n) // 取決於 n 的數值前進或後退
緩存路由組件
- 作用:讓不展示的路由組件保持掛載,不被銷毀
- 具體代碼:
1
2
3
4
5
6
7
8<!-- 使用 include 指定需要保持掛載的組件 -->
<keep-alive include="componentName">
<router-view></router-view>
</keep-alive>
<!-- 使用 include 指定多個組件 -->
<keep-alive :include="['component1','component2']">
<router-view></router-view>
</keep-alive>
路由的兩個生命週期鉤子
- 作用:路由組件所獨有的兩個鉤子,用於捕獲路由組件的激活狀態
- 具體代碼:
1
2
3
4
5
6
7
8
9// 路由組件被激活時觸發
activated(){
...
}
// 路由組件失去激活時觸發
deactivated(){
...
},
路由守衛
- 作用:對路由進行權限控制
- 全局路由守衛:用於監測所有路由切換
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// 全局前置路由守衛 ———— 初始化的時候被調用、每次路由切換之前被調用
router.beforeEach((to, from, next) => {
if(to.meta.isAuth){ // 判斷是否需要鑒定權限
if(localStorage.getItem('school') === 'CUHK'){
next() // 放行
}else{
alert('No auth to enter!')
}
}else{
next() // 放行
}
})
// 全局後置路由守衛 ———— 初始化的時候被調用、每次路由切換之後被調用
router.afterEach((to, from) => {
document.title = to.meta.title || 'Main Page'
})
- 獨享路由守衛:用於監測指定的路由切換
1
2
3
4
5
6
7
8
9
10
11beforeEnter: (to, from, next) => {
if(to.meta.isAuth){
if(localStorage.getItem('school') === 'CUHK'){
next()
}else{
alert('No auth to enter!')
}
}else{
next()
}
}- 備註:獨享路由守衛並沒有切換後的函數設置
- 組件內路由守衛
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// 進入路由守衛 ———— 通過路由規則,進入該組件時被調用
beforeRouteEnter(to, from, next){
if(to.meta.isAuth){ // 判斷是否需要鑒定權限
if(localStorage.getItem('school') === 'CUHK'){
next()
}else{
alert('No auth to enter!')
}
}else{
next()
}
},
// 離開路由守衛 ———— 通過路由規則,離開該組件時被調用
beforeRouteLeave(to, from, next){
next()
}
路由器的兩種工作模式
- 對於url而言,hash值就是 ———— #及其後面的內容
hash值不會包含在HTTP請求中,即:hash值不會帶給服務器
hash模式:
- 地址中永遠帶著 # 符號
- 若以後講地址通過第三方手機app分享,若app校驗嚴格,則地址會被標記為不合法
- 兼容性好
history模式:
- 地址美觀
- 兼容性和hash模式相比略差
- 應用部署上線時需要後端人員支持,解決刷新頁面服務器404問題
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.


