这篇文章主要为大家详细介绍了vue-router单页面路由的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
vue中,有一个类库叫做vue-router,是用来做单页面路由的。做路由一般分为四个步骤:
准备一个根组件vue.extend();
需要做路由的内容准备 template;
准备路由 new vuerouter();
关联路由 map
启动路由 start(app,'#box');//第一个参数是准备的根组件,第二个参数是要捆绑的位置就是自己定义的id中
github上vue-router下载地址:https://github.com/vuejs/vue-router
关于路由跳转的简单代码如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/vue-router.js" ></script>
<script type="text/javascript" src="js/vue-resource.js" ></script>
<title></title>
</head>
<body>
<p id="box">
<ul>
<li>
<a v-link="{path:'/home'}">我是第一个a</a>
</li>
<li >
<a v-link="{path:'news'}">我是第二个a</a>
</li>
</ul>
<p>
<router-view></router-view>
</p>
</p>
</body>
<script>
//1.准备一个根组件
var app=vue.extend();
//2.home news 组件准备
var home=vue.extend({
template:'<h3>我是第一个a的内容页</h3>'
});
var news=vue.extend({
template:'<h3>我是第二个a的内容页</h3>'
})
//3.准备路由
var router = new vuerouter();
//4.关联
router.map({
'home':{
component:home
},
'news':{
component:news
}
})
//5.启动路由
router.start(app,'#box');
</script>
</html>
以上就是vue-router单页面路由详解的详细内容。