Vue.js的路由,可以使不同内容(视图)之间快速切换而不需要刷新页面,所以非常适合做单页应用。用到 Vue-Router当然需要引入vue-router.js,要将vue-router.js放在vue.js的后面哦。
- <div id="app">
 - <aside>
 - <ul class="ulNav">
 - <li>
 - <router-link to="/index">首页</router-link>
 - </li>
 - <li>
 - <router-link to="/live">生活</router-link>
 - </li>
 - <li>
 - <router-link to="/activity">娱乐</router-link>
 - </li>
 - </ul>
 - </aside>
 - <content>
 - <!--路由链接对应的内容会渲染到这里-->
 - <router-view></router-view>
 - </content>
 - </div>
 - <script src="vue.js"></script>
 - <script src="vue-router.js"></script>
 - <script>
 - //创建三个路由模板
 - let indexComponent = {
 - template:
 - `<div>
 - 首页
 - </div>`
 - }
 - let liveComponent = {
 - template:
 - `<div>
 - 生活栏目
 - </div>`
 - }
 - let activityComponent = {
 - template:
 - `<div>
 - 娱乐栏目
 - </div>`
 - }
 - //声明路由
 - let router = new VueRouter({
 - routes : [
 - {
 - path: "/index",
 - component: indexComponent
 - },
 - {
 - path: "/live",
 - component : liveComponent
 - },
 - //路由别名
 - {
 - path: "/activity",
 - component : activityComponent,
 - alias: "/act"
 - },
 - //重定向,将首页/指向#/index
 - {
 - path: "/",
 - redirect: "/index",
 - alias: "/i"
 - }
 - ]
 - });
 - let vm = new Vue({
 - el: "#app",
 - //绑定路由
 - router
 - });
 - </script>
 
运行效果:

Vue.js重定向代码:
- {
 - path: "/",
 - redirect : "/index"
 - }
 
访问首页时,自动重定向到#/index,这个经常用到。
Vue.js路由别名:
- {
 - path: "/activity",
 - component : activityComponent,
 - alias: "/act"
 - }
 
别名alias,意思就是当用户访问/act时,浏览器的url会是/act,但页面的内容是/activity的内容,就像直接访问/activity一样。
