VueRouter 命名路由
1. 前言
本小節(jié)我們介紹如何使用 VueRouter 命名路由。包括如何定義命名路由、如何使用路由名實(shí)現(xiàn)路由跳轉(zhuǎn)。本節(jié)的學(xué)習(xí)內(nèi)容相對(duì)簡單,相信同學(xué)們看完本小節(jié),并對(duì)小節(jié)中的案例自己實(shí)現(xiàn)一遍就可以熟練掌握了。
2. 定義路由名
在之前的小節(jié)中,我們學(xué)習(xí)了如何定義一個(gè)路由:
const router = new VueRouter({
routes: [
{
path: '/user',
component: '[component-name]'
}
]
})
route 對(duì)象中有兩個(gè)屬性。path 表示路由地址,component 表示路由顯示的組件。我們可以在 route 對(duì)象中添加一個(gè) name 屬性用來給路由指定一個(gè)名字:
const router = new VueRouter({
routes: [
{
path: '/user',
name: 'user',
component: '[component-name]'
}
]
})
2.1 <router-link>
跳轉(zhuǎn)命名路由
在之前的小節(jié)中,我們學(xué)習(xí)了使用 <router-link to="path">...</router-link>
的方式來實(shí)現(xiàn)路由跳轉(zhuǎn)。實(shí)際上 router-link
的 to 屬性可以接收一個(gè)對(duì)象:
<router-link :to="{path: 'path'}">...</router-link>
讓我們來看一個(gè)簡單的示例:
<!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,歡迎使用慕課網(wǎng)學(xué)習(xí) Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
template: `<ul><li>1. Vue 計(jì)算屬性的學(xué)習(xí)</li><li>2. React 基礎(chǔ)學(xué)習(xí)</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 行,我們定義了首頁跳轉(zhuǎn)鏈接,通過對(duì)象的形式給屬性 to 賦值。
HTML 代碼第 13 行,我們定義了文章跳轉(zhuǎn)鏈接,通過字符串的形式給屬性 to 賦值。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-16 行,我們定義了路由數(shù)組:
- 1. 首頁路由,地址為 ‘/index’,匹配組件 Index。
- 2. 文章路由,地址為 ‘/article’,匹配組件 Article。
JS 代碼第 18-20 行,創(chuàng)建 router 實(shí)例,然后傳 routes
配置。
JS 代碼第 24 行,通過 router 配置參數(shù)注入路由。
除了通過 path 可以鏈接到路由外,還可以通過路由 name 實(shí)現(xiàn)鏈接跳轉(zhuǎn):
`<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,歡迎使用慕課網(wǎng)學(xué)習(xí) Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
template: `<ul><li>1. Vue 計(jì)算屬性的學(xué)習(xí)</li><li>2. React 基礎(chǔ)學(xué)習(xí)</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 行,我們定義了兩個(gè)跳轉(zhuǎn)鏈接,通過對(duì)象的形式給屬性 to 賦值,跳轉(zhuǎn)指定 name 的路由。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-16 行,我們定義了路由數(shù)組:
-
- 首頁路由,地址為 ‘/index’, 路由名為 index,匹配組件 Index。
-
- 文章路由,地址為 ‘/article’, 路由名為 article,匹配組件 Article。
JS 代碼第 18-20 行,創(chuàng)建 router 實(shí)例,然后傳 routes
配置。
JS 代碼第 24 行,通過 router 配置參數(shù)注入路由。
2.2 編程式導(dǎo)航跳轉(zhuǎn)命名路由
在之前的小節(jié)中,我們學(xué)習(xí)了如何使用 $router 實(shí)例來實(shí)現(xiàn)編程式的導(dǎo)航。我們也可以使用 $router 實(shí)例跳轉(zhuǎn)指定名字的路由地址:
<!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,歡迎使用慕課網(wǎng)學(xué)習(xí) Vue 教程!</div>',
})
const Article = Vue.component('myArticle', {
template: `<ul><li>1. Vue 計(jì)算屬性的學(xué)習(xí)</li><li>2. Vue 偵聽器的學(xué)習(xí)</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 行,我們定義了兩個(gè)按鈕,并給他們點(diǎn)擊事件 jump。
HTML 代碼第 15 行,我們使用 <router-view></router-view>
組件來渲染匹配組件。
JS 代碼第 5-7 行,我們定義了組件 Index。
JS 代碼第 9-11 行,我們定義了組件 Article。
JS 代碼第 13-16 行,我們定義了路由數(shù)組:
1. 首頁路由,地址為 ‘/index’,匹配組件 Index。
2. 文章路由,地址為 ‘/article’,匹配組件 Article。
JS 代碼第 18-20 行,創(chuàng)建 router 實(shí)例,然后傳 routes
配置。
JS 代碼第 24 行,通過 router 配置參數(shù)注入路由。
JS 代碼第 29-31 行,我們定義來 jump 函數(shù),通過 router.push 實(shí)現(xiàn)路由跳轉(zhuǎn)。
3. 小結(jié)
本節(jié),我們帶大家學(xué)習(xí)了 VueRouter 命名路由的使用方法。主要知識(shí)點(diǎn)有以下幾點(diǎn):
- 通過 name 屬性指定路由名稱。
- 通過
<router-link>
跳轉(zhuǎn)指定名稱的路由地址。 - 通過 $router 跳轉(zhuǎn)指定名稱的路由地址。