刚学习vue不久,就接触了路由这个好东西。下面简单聊聊vue-router的基本用法。
一、路由的概念
路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的news按钮,页面中就要显示news的内容。Home按钮 => home 内容, news按钮 => news内容,也可以说是一种映射. 所以在页面上有两个部分,一个是点击部分,一个是点击之后,显示内容的部分。
点击之后,怎么做到正确的对应,比如,我点击home 按钮,页面中怎么就正好能显示home的内容。这就要在js 文件中配置路由。
路由中有三个基本的概念 route, routes, router。
1、 route,它是一条路由,由这个英文单词也可以看出来,它是单数, Home按钮 => home内容, 这是一条route, news按钮 => news内容, 这是另一条路由。
2、 routes 是一组路由,把上面的每一条路由组合起来,形成一个数组。[{home 按钮 =>home内容 }, { news按钮 => news内容}]
3、 router 是一个机制,相当于一个管理者,它来管理路由。因为routes 只是定义了一组路由,它放在哪里是静止的,当真正来了请求,怎么办? 就是当用户点击home 按钮的时候,怎么办?这时router 就起作用了,它到routes 中去查找,去找到对应的 home 内容,所以页面中就显示了 home 内容。
4、客户端中的路由,实际上就是dom 元素的显示和隐藏。当页面中显示home 内容的时候,news中的内容全部隐藏,反之也是一样。客户端路由有两种实现方式:基于hash 和基于html5 history api.
vue-router中的路由也是基于上面的内容来实现的
在vue中实现路由还是相对简单的。因为我们页面中所有内容都是组件化的,我们只要把路径和组件对应起来就可以了,然后在页面中把组件渲染出来。
5、router-view作用: router-view可以
当做是一个容器,它渲染的组件是你使用 vue-router 指定的
二、vue-router基础使用
1、下载vue和vue-router
此案例用的是vue@1.0.28、vue-router@0.7.13
注意,最新版本的vue和vue-router不支持map。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue-router</title>
<script src="../../node_modules/vue/dist/vue.min.js"></script>
<script src="../../node_modules/vue-router/dist/vue-router.min.js"></script> <style>
body,ul,li,a{
padding: 0;
margin: 0;
} ul{
list-style: none;
overflow: hidden;
}
a{
text-decoration: none;
} #box{
width: 600px;
margin: 100px auto; }
#box ul{
padding: 0 100px;
background-color: #2dc3e8;
} #box ul li a{
display: block;
width: 100px;
height: 50px;
background-color: #2dc3e8;
color: #fff;
float: left;
line-height:50px;
text-align: center;
}
#box ul li:hover{
background-color: #00b3ee;
} #box ul li a.v-link-active{
font-size: 18px;
background-color: #00b3ee;
} </style>
</head>
<body>
<div id="box">
<ul>
<li><a v-link="{path:'/home'}">主页</a></li>
<li><a v-link="{path: '/news'}">新闻中心</a></li>
<li><a v-link="{path: '/product'}">最新产品</a></li>
<li><a v-link="{path: '/activity'}">促销活动</a></li>
</ul> <div>
<router-view></router-view>
</div>
</div> <script>
// 1.根组件
var App = Vue.extend(); // 2.准备需要的组件
var Home = Vue.extend({
template: '<h3>我是主页</h3>'
}); var News = Vue.extend({
template: '<h3>我是新闻</h3>'
}); var Product = Vue.extend({
template: '<h3>我是产品</h3>'
}); var Activity = Vue.extend({
template: '<h3>我是促销活动</h3>'
}); // 3.准备路由
var router = new VueRouter(); // 4.关联
router.map({
'home': {
component: Home
},
'news': {
component: News
},
'product': {
component: Product
},
'activity': {
component: Activity
} }); // 5.启动路由
router.start(App, '#box');
// 6.默认跳转
router.redirect({
'/':'/home'
}); </script>
</body>
</html>
运行结果:
三 、路由嵌套
在实际开发中单路由跳转不能满足我们的需求,常常需要用到多个路由嵌套,下面是简单的路由嵌套demo.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue-router-嵌套</title>
<script src="../../node_modules/vue/dist/vue.min.js"></script>
<script src="../../node_modules/vue-router/dist/vue-router.min.js"></script> <style> body,ul,li,a{
padding: 0;
margin: 0;
} ul{
list-style: none;
overflow: hidden;
}
a{
text-decoration: none;
} #box{
width: 600px;
margin: 100px auto; }
#box ul{
padding: 0 100px;
background-color: #2dc3e8;
} #box ul li a{
display: block;
width: 100px;
height: 50px;
background-color: #2dc3e8;
color: #fff;
float: left;
line-height:50px;
text-align: center;
}
#box ul li:hover{
background-color: #00b3ee;
} #box ul li a.v-link-active{
font-size: 18px;
background-color: #00b3ee;
} </style> </head>
<body>
<div id="box">
<ul>
<li><a v-link="{path:'/home'}">主页</a></li>
<li><a v-link="{path: '/news'}">新闻中心</a></li>
</ul> <div>
<router-view></router-view>
</div>
</div> <template id="home">
<h3>我是主页</h3>
<div>
<a v-link="{path: '/home/login/lele'}">登录</a>
<a v-link="{path: '/home/register'}">注册</a>
</div>
<div>
<router-view></router-view>
</div>
</template> <template id="news">
<h3>我是新闻</h3>
<div>
<a v-link="{path: '/news/detail/001'}">新闻001</a>
<a v-link="{path: '/news/detail/002'}">新闻002</a>
</div>
<router-view></router-view>
</template> <template id="detail">
<!--当前参数-->
{{$route.params |json}}
<br>
<!--当前路径-->
{{$route.path}}
<br>
<!--当前数据-->
{{$route.query |json}}
</template> <script>
// 1.根组件
var App = Vue.extend(); // 2.准备需要的组件
var Home = Vue.extend({
template: '#home'
}); var News = Vue.extend({
template: '#news'
}); var Detail = Vue.extend({
template:'#detail'
}); // 3.准备路由
var router = new VueRouter(); // 4.关联
router.map({
'home': {
component: Home,
subRoutes:{
'login/:name': {
component:{
template: '<strong>我是登录信息{{$route.params |json}}</strong>'
}
},
'register': {
component:{
template: '<strong>我是注册信息</strong>'
}
}
}
},
'news': {
component: News,
subRoutes: {
'/detail/:id': {
component: Detail
}
}
}
}); // 5.启动路由
router.start(App, '#box'); // 6.跳转
router.redirect({
'/':'/home'
}); </script>
</body>
</html>
运行结果: