vue一個重要的方面就是路由,下面是本身寫的一個路由的例子:javascript
一、引入依賴庫就沒必要再說html
二、建立組件vue
兩種寫法java
第一種:間接 <template id="home"> <div> <h1>Home</h1> <p>{{msg}}</p> </div> </template> var About = Vue.extend({ template: '#about' }); 第二種:直接 var Out = Vue.extend({ template: '<div><h1>Out</h1><p>This is the tutorial out vue-router.</p></div>' });
三、建立 router 實例,傳 'routes'路由映射配置git
var router = new VueRouter({ routes: [ { path: '/路徑', component: 組件名 }, { path: '/', component: 組件名}, //設置默認路徑 { path: "*", component:Home }//路徑不存在
] });
四、建立和掛載根實例。記得要經過 router 配置參數注入路由,從而讓整個應用都有路由功能github
var vm = new Vue({ router: router }).$mount('#app');
總體的demovue-router
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hello world</title> </head> <body> <div id="app"> <div> <!-- 四、<router-link>默認會被渲染成一個 `<a>` 標籤 ,to指令跳轉到指定路徑 --> <router-link to="/home">Go to Home</router-link> <router-link to="/about">Go to About</router-link> <router-link to="/out">Go to Out</router-link> </div> <!-- 五、在頁面上使用<router-view></router-view>標籤,用於渲染匹配的組件 --> <!--這裏顯示的是展現的界面--> <router-view></router-view> </div> <template id="home"> <div> <h1>Home</h1> <p>{{msg}}</p> </div> </template> <template id="about"> <div> <h1>about</h1> <p>This is the tutorial about vue-router.</p> </div> </template> <!-- 0、引入依賴庫 --> <script src="../js/vue2.0.js" type="text/javascript" charset="utf-8"></script> <script src="lib/vue-router.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> /* 一、建立組件 */ var Home = Vue.extend({ template: '#home', data: function() { return { msg: 'Hello, vue router!' } } }); var About = Vue.extend({ template: '#about' }); var Out = Vue.extend({ template: '<div><h1>Out</h1><p>This is the tutorial out vue-router.</p></div>' }); // 2. 建立 router 實例,而後傳 `routes`路由映射 配置 var router = new VueRouter({ routes: [ { path: '/home', component: Home }, { path: '/about', component: About }, { path: '/out', component: Out }, {path: '/', component: Home },//設置默認路徑 { path: "*", component:Home }//路徑不存在 ] }); // 3. 建立和掛載根實例。記得要經過 router 配置參數注入路由,從而讓整個應用都有路由功能 var vm = new Vue({ router: router }).$mount('#app'); // 如今,應用已經啓動了! </script> </body> </html>
關於路由嵌套app
在配置routes映射時添加children配置學習
以下:阿里雲
var router = new VueRouter({ routes:[ {path:'/home',component:Home, children:[//子路由 {path:'news',component:News}, {path:'change',component:change} ]}, {path:'/me',component:Me}, {path:'/',component:Me} ] })
關於具體的demo能夠參考GitHub上,另外還總結了一些本身最近在學習的阿里雲上傳圖片等,會逐步更新,敬請指教!
轉載請註明出處,謝謝合做