1 <html> 2 <head> 3 <meta charset="utf-8"> 4 <title>vue入門案例</title> 5 <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script> 6 <script src="https://cdn.bootcss.com/vue-router/3.1.2/vue-router.js"></script> 7 </head> 8 <body> 9 <div id="app"> 10 <router-link to="/Home" replace>首頁</router-link> 11 <router-link to="/Abort" replace>關於本站</router-link> 12 <router-view></router-view> 13 14 </div> 15 </body> 16 <script type="text/javascript"> 17 //建立組件 18 const Home = Vue.extend({ 19 template: '<div><p>網站首頁</p><div>Me*源的博客內容</div></div>' 20 }); 21 22 const Abort = Vue.extend({ 23 template: '<div><p>關於本站</p><div>博主相關信息,運營內容</div></div>' 24 }); 25 //添加url與組件的映射關係(組合路由) 26 let routes = [ 27 { 28 path:'/', 29 component:Home 30 }, 31 { 32 path:'/Home', 33 component:Home 34 }, 35 { 36 path:'/Abort', 37 component:Abort 38 }, 39 ] 40 41 //將路由的集合組合成路由器 42 const router = new VueRouter({routes}) 43 44 45 new Vue({ 46 el: "#app", 47 router, 48 data: { 49 msg: 'hello Vue!!' 50 } 51 }) 52 </script> 53 54 </html>