剛學習vue不久,就接觸了路由這個好東西。下面簡單聊聊vue-router的基本用法。html
路由,其實就是指向的意思,當我點擊頁面上的home按鈕時,頁面中就要顯示home的內容,若是點擊頁面上的news按鈕,頁面中就要顯示news的內容。Home按鈕 => home 內容, news按鈕 => news內容,也能夠說是一種映射. 因此在頁面上有兩個部分,一個是點擊部分,一個是點擊以後,顯示內容的部分。
點擊以後,怎麼作到正確的對應,好比,我點擊home 按鈕,頁面中怎麼就正好能顯示home的內容。這就要在js 文件中配置路由。
路由中有三個基本的概念 route, routes, router。
一、 route,它是一條路由,由這個英文單詞也能夠看出來,它是單數, Home按鈕 => home內容, 這是一條route, news按鈕 => news內容, 這是另外一條路由。
二、 routes 是一組路由,把上面的每一條路由組合起來,造成一個數組。[{home 按鈕 =>home內容 }, { news按鈕 => news內容}]
三、 router 是一個機制,至關於一個管理者,它來管理路由。由於routes 只是定義了一組路由,它放在哪裏是靜止的,當真正來了請求,怎麼辦? 就是當用戶點擊home 按鈕的時候,怎麼辦?這時router 就起做用了,它到routes 中去查找,去找到對應的 home 內容,因此頁面中就顯示了 home 內容。
四、客戶端中的路由,實際上就是dom 元素的顯示和隱藏。當頁面中顯示home 內容的時候,news中的內容所有隱藏,反之也是同樣。客戶端路由有兩種實現方式:基於hash 和基於html5 history api.
vue-router中的路由也是基於上面的內容來實現的
在vue中實現路由仍是相對簡單的。由於咱們頁面中全部內容都是組件化的,咱們只要把路徑和組件對應起來就能夠了,而後在頁面中把組件渲染出來。vue
五、router-view做用: router-view能夠
當作是一個容器,它渲染的組件是你使用 vue-router 指定的html5
此案例用的是vue@1.0.28、vue-router@0.7.13node
注意,最新版本的vue和vue-router不支持map。vue-router
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Vue-router</title> 6 <script src="../../node_modules/vue/dist/vue.min.js"></script> 7 <script src="../../node_modules/vue-router/dist/vue-router.min.js"></script> 8 9 10 <style> 11 body,ul,li,a{ 12 padding: 0; 13 margin: 0; 14 } 15 16 ul{ 17 list-style: none; 18 overflow: hidden; 19 } 20 a{ 21 text-decoration: none; 22 } 23 24 #box{ 25 width: 600px; 26 margin: 100px auto; 27 28 } 29 #box ul{ 30 padding: 0 100px; 31 background-color: #2dc3e8; 32 } 33 34 #box ul li a{ 35 display: block; 36 width: 100px; 37 height: 50px; 38 background-color: #2dc3e8; 39 color: #fff; 40 float: left; 41 line-height:50px; 42 text-align: center; 43 } 44 #box ul li:hover{ 45 background-color: #00b3ee; 46 } 47 48 #box ul li a.v-link-active{ 49 font-size: 18px; 50 background-color: #00b3ee; 51 } 52 53 </style> 54 </head> 55 <body> 56 <div id="box"> 57 <ul> 58 <li><a v-link="{path:'/home'}">主頁</a></li> 59 <li><a v-link="{path: '/news'}">新聞中心</a></li> 60 <li><a v-link="{path: '/product'}">最新產品</a></li> 61 <li><a v-link="{path: '/activity'}">促銷活動</a></li> 62 </ul> 63 64 65 66 <div> 67 <router-view></router-view> 68 </div> 69 </div> 70 71 72 <script> 73 // 1.根組件 74 var App = Vue.extend(); 75 76 // 2.準備須要的組件 77 var Home = Vue.extend({ 78 template: '<h3>我是主頁</h3>' 79 }); 80 81 var News = Vue.extend({ 82 template: '<h3>我是新聞</h3>' 83 }); 84 85 var Product = Vue.extend({ 86 template: '<h3>我是產品</h3>' 87 }); 88 89 var Activity = Vue.extend({ 90 template: '<h3>我是促銷活動</h3>' 91 }); 92 93 // 3.準備路由 94 var router = new VueRouter(); 95 96 // 4.關聯 97 router.map({ 98 'home': { 99 component: Home 100 }, 101 'news': { 102 component: News 103 }, 104 'product': { 105 component: Product 106 }, 107 'activity': { 108 component: Activity 109 } 110 111 }); 112 113 114 115 // 5.啓動路由 116 router.start(App, '#box'); 117 118 // 6.默認跳轉 119 router.redirect({ 120 '/':'/home' 121 }); 122 123 124 </script> 125 </body> 126 </html>
在實際開發中單路由跳轉不能知足咱們的需求,經常須要用到多個路由嵌套,下面是簡單的路由嵌套demo.json
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Vue-router-嵌套</title> 6 <script src="../../node_modules/vue/dist/vue.min.js"></script> 7 <script src="../../node_modules/vue-router/dist/vue-router.min.js"></script> 8 9 <style> 10 11 body,ul,li,a{ 12 padding: 0; 13 margin: 0; 14 } 15 16 ul{ 17 list-style: none; 18 overflow: hidden; 19 } 20 a{ 21 text-decoration: none; 22 } 23 24 #box{ 25 width: 600px; 26 margin: 100px auto; 27 28 } 29 #box ul{ 30 padding: 0 100px; 31 background-color: #2dc3e8; 32 } 33 34 #box ul li a{ 35 display: block; 36 width: 100px; 37 height: 50px; 38 background-color: #2dc3e8; 39 color: #fff; 40 float: left; 41 line-height:50px; 42 text-align: center; 43 } 44 #box ul li:hover{ 45 background-color: #00b3ee; 46 } 47 48 #box ul li a.v-link-active{ 49 font-size: 18px; 50 background-color: #00b3ee; 51 } 52 53 </style> 54 55 </head> 56 <body> 57 <div id="box"> 58 <ul> 59 <li><a v-link="{path:'/home'}">主頁</a></li> 60 <li><a v-link="{path: '/news'}">新聞中心</a></li> 61 </ul> 62 63 <div> 64 <router-view></router-view> 65 </div> 66 </div> 67 68 <template id="home"> 69 <h3>我是主頁</h3> 70 <div> 71 <a v-link="{path: '/home/login/lele'}">登陸</a> 72 <a v-link="{path: '/home/register'}">註冊</a> 73 </div> 74 <div> 75 <router-view></router-view> 76 </div> 77 </template> 78 79 <template id="news"> 80 <h3>我是新聞</h3> 81 <div> 82 <a v-link="{path: '/news/detail/001'}">新聞001</a> 83 <a v-link="{path: '/news/detail/002'}">新聞002</a> 84 </div> 85 <router-view></router-view> 86 </template> 87 88 <template id="detail"> 89 <!--當前參數--> 90 {{$route.params |json}} 91 <br> 92 <!--當前路徑--> 93 {{$route.path}} 94 <br> 95 <!--當前數據--> 96 {{$route.query |json}} 97 </template> 98 99 <script> 100 // 1.根組件 101 var App = Vue.extend(); 102 103 // 2.準備須要的組件 104 var Home = Vue.extend({ 105 template: '#home' 106 }); 107 108 var News = Vue.extend({ 109 template: '#news' 110 }); 111 112 var Detail = Vue.extend({ 113 template:'#detail' 114 }); 115 116 // 3.準備路由 117 var router = new VueRouter(); 118 119 // 4.關聯 120 router.map({ 121 'home': { 122 component: Home, 123 subRoutes:{ 124 'login/:name': { 125 component:{ 126 template: '<strong>我是登陸信息{{$route.params |json}}</strong>' 127 } 128 }, 129 'register': { 130 component:{ 131 template: '<strong>我是註冊信息</strong>' 132 } 133 } 134 } 135 }, 136 'news': { 137 component: News, 138 subRoutes: { 139 '/detail/:id': { 140 component: Detail 141 } 142 } 143 } 144 }); 145 146 147 148 // 5.啓動路由 149 router.start(App, '#box'); 150 151 // 6.跳轉 152 router.redirect({ 153 '/':'/home' 154 }); 155 156 157 </script> 158 </body> 159 </html>
運行結果:api