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上,另外还总结了一些本身最近在学习的阿里云上传图片等,会逐步更新,敬请指教!
转载请注明出处,谢谢合做