<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>vue-router</title> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css"> <script src="js/vue.js" ></script> <!--- 第一步,將vue-router的原始碼引入---> <script src="js/vue-router.js" ></script></head><body><div id="app"> <div class="container"> <ul class="nav nav-pills"> <!--- 使用router-link 進行跳轉 ---> <li><router-link to="/index/i">首頁</router-link></li> <li><router-link to="/index/b">圖書列表</router-link></li> <li><router-link to="/index/g">遊戲列表</router-link></li> </ul> <div> <router-view></router-view> </div> </div></div><script> /** * 配置路由,使用官方的VueRouter * new VueRouter 得到一個VueRouter的執行個體 * 在VueRouter中傳遞參數(對象) * 在參數中有個routes的數組屬性 * 這個數組是一個對象數組 * 每一個對象就是一個路由狀態 */ var router = new VueRouter({ routes : [ { path : "/index", component : { template : `//這裡用了反(單引號)可以有代碼提醒 也可以用正常的單引號 <div> <h1>首頁</h1> <router-view></router-view> </div> `// template : "<div><router-view></router-view></div>" 另外一種寫法 }, children : [ { path : "i", component : { template : "<h1>這個是嵌套的首頁</h1>" } }, { path : "b", component : { template : "<h1>這個是嵌套的圖書列表</h1>" } }, { path : "g", component : { template : "<h1>這個是嵌套的遊戲列表</h1>" } } ] } ] }); new Vue({ data : { username : "劉德華" }, router : router }).$mount("#app");</script></body></html>