VueRouter路由別名、重定向
1. 前言
本小節我們介紹如何使用 VueRouter 路由別名、重定向。路由別名和重定向在項目中經常使用,本節的學習內容相對簡單,相信同學們看完本小節,并對小節中的案例自己實現一遍就可以熟練掌握了。
2. 路由重定向
重定向也是通過 routes 配置來完成,可以配置路由重定向到具體路由地址、具名路由或者動態返回重定向目標。
2.1 重定向到路由地址
通過屬性 redirect 指定重定向的路由地址:
const router = new VueRouter({
routes: [
{ path: '/a', redirect: '/b' }
]
})
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<div>
<router-link to="/index">首頁</router-link>
<router-link to="/article">文章</router-link>
</div>
<router-view></router-view>
</div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">
const Index = Vue.component('index', {
template: '<div>Hello,歡迎使用慕課網學習 Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
template: `<ul><li>1. Vue 計算屬性的學習</li><li>2. React 基礎學習</li></ul>`,
})
const routes = [
{ path: '/', redirect: '/index' },
{ path: '/index', component: Index },
{ path: '/article', component: Article }
]
const router = new VueRouter({
routes: routes
})
var vm = new Vue({
el: '#app',
router: router,
data() {
return {}
}
})
</script>
</html>
代碼解釋:
HTML 代碼第 12-13 行,我們定義了兩個跳轉鏈接。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-17 行,我們定義了路由數組:
- 根路由,地址為 ‘/’,重定向到路由地址 ‘/index’。
- 首頁路由,地址為 ‘/index’,匹配組件 Index。
- 文章路由,地址為 ‘/article’,匹配組件 Article。
JS 代碼第 19-21 行,創建 router 實例,然后傳 routes
配置。
JS 代碼第 25 行,通過 router 配置參數注入路由。
2.2 重定向到具名路由
通過屬性 redirect 重定向到具名路由:
const router = new VueRouter({
routes: [
{ path: '/a', redirect: {name: 'name'} }
]
})
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<div>
<router-link to="/index">首頁</router-link>
<router-link to="/article">文章</router-link>
</div>
<router-view></router-view>
</div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">
const Index = Vue.component('index', {
template: '<div>Hello,歡迎使用慕課網學習 Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
template: `<ul><li>1. Vue 計算屬性的學習</li><li>2. React 基礎學習</li></ul>`,
})
const routes = [
{ path: '/', redirect: {name: 'index'} },
{ path: '/index', name: 'index', component: Index },
{ path: '/article', name: 'article', component: Article }
]
const router = new VueRouter({
routes: routes
})
var vm = new Vue({
el: '#app',
router: router,
data() {
return {}
}
})
</script>
</html>
代碼解釋:
HTML 代碼第 12-13 行,我們定義了兩個跳轉鏈接。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-17 行,我們定義了路由數組:
- 根路由,地址為 ‘/’,重定向到具名路由 ‘index’。
- 首頁路由,地址為 ‘/index’,匹配組件 Index。
- 文章路由,地址為 ‘/article’,匹配組件 Article。
JS 代碼第 19-21 行,創建 router 實例,然后傳 routes
配置。
JS 代碼第 25 行,通過 router 配置參數注入路由。
2.3 動態返回重定向目標
屬性 redirect 可以接收一個方法,動態返回重定向目標:
const router = new VueRouter({
routes: [
{ path: '/a', redirect: to => {
// 方法接收 目標路由 作為參數
// return 重定向的 字符串路徑/路徑對象
}}
]
})
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<div>
<router-link to="/index">首頁</router-link>
<router-link to="/article">文章</router-link>
</div>
<router-view></router-view>
</div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">
const Index = Vue.component('index', {
template: '<div>Hello,歡迎使用慕課網學習 Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
template: `<ul><li>1. Vue 計算屬性的學習</li><li>2. React 基礎學習</li></ul>`,
})
const routes = [
{ path: '/', redirect: to => {
if(Math.random() > 0.5) {
return '/index'
}else {
return {
name: 'article'
}
}
}},
{ path: '/index', name: 'index', component: Index },
{ path: '/article', name: 'article', component: Article }
]
const router = new VueRouter({
routes: routes
})
var vm = new Vue({
el: '#app',
router: router,
data() {
return {}
}
})
</script>
</html>
代碼解釋:
HTML 代碼第 12-13 行,我們定義了兩個跳轉鏈接。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-25 行,我們定義了路由數組:
- 根路由,地址為 ‘/’,根據隨機數的大小重定向到路由 ‘/index’ 或 ‘/article’。
- 首頁路由,地址為 ‘/index’,匹配組件 Index。
- 文章路由,地址為 ‘/article’,匹配組件 Article。
JS 代碼第 27-29 行,創建 router 實例,然后傳 routes
配置。
JS 代碼第 32 行,通過 router 配置參數注入路由。
3. 路由別名
“重定向”的意思是,當用戶訪問 /a 時,URL 將會被替換成 /b,然后匹配路由為 /b,那么“別名”又是什么呢?
/a 的別名是 /b,意味著,當用戶訪問 /b 時,URL 會保持為 /b,但是路由匹配則為 /a,就像用戶訪問 /a 一樣。
const router = new VueRouter({
routes: [
{ path: '/a', component: A, alias: '/b' }
]
})
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<div>
<router-link to="/index">首頁</router-link>
<router-link to="/article">文章</router-link>
</div>
<router-view></router-view>
</div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script type="text/javascript">
const Index = Vue.component('index', {
template: '<div>Hello,歡迎使用慕課網學習 Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
template: `<ul><li>1. Vue 計算屬性的學習</li><li>2. React 基礎學習</li></ul>`,
})
const routes = [
{ path: '/index', component: Index, alias: '/' },
{ path: '/article', component: Article }
]
const router = new VueRouter({
routes: routes
})
var vm = new Vue({
el: '#app',
router: router,
data() {
return {}
}
})
</script>
</html>
代碼解釋:
HTML 代碼第 12-13 行,我們定義了兩個跳轉鏈接。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-16 行,我們定義了路由數組:
- 首頁路由,地址為 ‘/index’,匹配組件 Index,路由別名 ‘/’。
- 文章路由,地址為 ‘/article’,匹配組件 Article。
JS 代碼第 18-20 行,創建 router 實例,然后傳 routes
配置。
JS 代碼第 24 行,通過 router 配置參數注入路由。
4. 小結
本節,我們帶大家學習了 VueRouter 路由重定向和別名。主要知識點有以下幾點:
- 通過 redirect 屬性指定路由重定向地址。
- 通過 alias 屬性配置路由別名。