VueRouter 命名路由
1. 前言
本小節我們介紹如何使用 VueRouter 命名路由。包括如何定義命名路由、如何使用路由名實現路由跳轉。本節的學習內容相對簡單,相信同學們看完本小節,并對小節中的案例自己實現一遍就可以熟練掌握了。
2. 定義路由名
在之前的小節中,我們學習了如何定義一個路由:
const router = new VueRouter({
routes: [
{
path: '/user',
component: '[component-name]'
}
]
})
route 對象中有兩個屬性。path 表示路由地址,component 表示路由顯示的組件。我們可以在 route 對象中添加一個 name 屬性用來給路由指定一個名字:
const router = new VueRouter({
routes: [
{
path: '/user',
name: 'user',
component: '[component-name]'
}
]
})
2.1 <router-link>
跳轉命名路由
在之前的小節中,我們學習了使用 <router-link to="path">...</router-link>
的方式來實現路由跳轉。實際上 router-link
的 to 屬性可以接收一個對象:
<router-link :to="{path: 'path'}">...</router-link>
讓我們來看一個簡單的示例:
<!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="{path: '/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 },
{ path: '/article', component: Article }
]
const router = new VueRouter({
routes: routes
})
var vm = new Vue({
el: '#app',
router: router,
data() {
return {}
}
})
</script>
</html>
代碼解釋:
HTML 代碼第 12 行,我們定義了首頁跳轉鏈接,通過對象的形式給屬性 to 賦值。
HTML 代碼第 13 行,我們定義了文章跳轉鏈接,通過字符串的形式給屬性 to 賦值。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-16 行,我們定義了路由數組:
- 1. 首頁路由,地址為 ‘/index’,匹配組件 Index。
- 2. 文章路由,地址為 ‘/article’,匹配組件 Article。
JS 代碼第 18-20 行,創建 router 實例,然后傳 routes
配置。
JS 代碼第 24 行,通過 router 配置參數注入路由。
除了通過 path 可以鏈接到路由外,還可以通過路由 name 實現鏈接跳轉:
`<router-link :to="{name: 'name'}">...</router-link>`
<!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="{name: 'index'}">首頁</router-link>
<router-link :to="{name: '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', 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 行,我們定義了兩個跳轉鏈接,通過對象的形式給屬性 to 賦值,跳轉指定 name 的路由。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-16 行,我們定義了路由數組:
-
- 首頁路由,地址為 ‘/index’, 路由名為 index,匹配組件 Index。
-
- 文章路由,地址為 ‘/article’, 路由名為 article,匹配組件 Article。
JS 代碼第 18-20 行,創建 router 實例,然后傳 routes
配置。
JS 代碼第 24 行,通過 router 配置參數注入路由。
2.2 編程式導航跳轉命名路由
在之前的小節中,我們學習了如何使用 $router 實例來實現編程式的導航。我們也可以使用 $router 實例跳轉指定名字的路由地址:
<!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>
<button @click="jump('index')">首頁</button>
<button @click="jump('article')">文章</button>
</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. Vue 偵聽器的學習</li></ul>`,
})
const routes = [
{ 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,
data() {
return {}
},
methods: {
jump(name) {
this.$router.push({
name: name
})
}
}
})
</script>
</html>
代碼解釋:
HTML 代碼第 12-13 行,我們定義了兩個按鈕,并給他們點擊事件 jump。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-16 行,我們定義了路由數組:
1. 首頁路由,地址為 ‘/index’,匹配組件 Index。
2. 文章路由,地址為 ‘/article’,匹配組件 Article。
JS 代碼第 18-20 行,創建 router 實例,然后傳 routes
配置。
JS 代碼第 24 行,通過 router 配置參數注入路由。
JS 代碼第 29-31 行,我們定義來 jump 函數,通過 router.push 實現路由跳轉。
3. 小結
本節,我們帶大家學習了 VueRouter 命名路由的使用方法。主要知識點有以下幾點:
- 通過 name 屬性指定路由名稱。
- 通過
<router-link>
跳轉指定名稱的路由地址。 - 通過 $router 跳轉指定名稱的路由地址。