VueRouter命名路由
定义路由名
route 对象中有两个属性。path 表示路由地址,component 表示路由显示的组件。我们可以在 route 对象中添加一个 name 属性用来给路由指定一个名字:
const router = new VueRouter({
routes: [
{
path: '/user',
name: 'user',
component: '[component-name]'
}
]
})
<router-link> 跳转命名路由
在之前的小节中,我们学习了使用
<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>
除了通过 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>
编程式导航跳转命名路由
在之前的小节中,我们学习了如何使用 $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>