vue-router实现组件间的跳转(参数传递)
2017-11-07 10:56
1146 查看
通过VueRouter来实现组件之间的跳转:参数的传递,具体内容如下
login ---用户名--->main
①明确发送方和接收方
②配置接收方的路由地址
{path:'/myTest',component:TestComponent}
-->
{path:'/myTest/:id',component:TestComponent}
③接收方获取传递来的数据
this.$route.params.id
④跳转的时候,发送参数
this.$router.push('/myTest/20')
<router-link :to="'/myTest'+id">跳转</router-link>
代码:
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>传参</title> <script src="js/vue.js"></script> <script src="js/vue-router.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <!--指定容器 --> <router-view></router-view> </div> <script> //创建主页面组件 var myMain = Vue.component("main-component",{ //保存登录传递过来的数据 data:function(){ return { uName:'' } }, template:` <div> <h1>主页面用户名:{{uName}}</h1> </div> `, //挂载该组件时自动拿到数据 beforeMount:function(){ //接收参数 console.log(this.$route.params); this.uName = this.$route.params.myName ; } }) //创建登录页面组件 var myLogin = Vue.component("login-component",{ //保存用户输入的数据 data:function(){ return { userInput:"" } }, methods:{ toMain:function(){ //跳转到主页面,并将用户输入的名字发送过去 this.$router.push("/main/"+this.userInput); console.log(this.userInput); } }, template:` <div> <h1>登录页面</h1> <input type="text" v-model="userInput" placeholder="请输入用户名"> <button @click="toMain">登录到主页面</button> <br> <router-link :to="'/main/'+userInput">登录到主页面</router-link> </div> ` }) var NotFound = Vue.component("not-found",{ template:` <div> <h1>404 Page Not Found</h1> <router-link to="/login">返回登录页</router-link> </div> ` }) //配置路由词典 const myRoutes = [ {path:"",component:myLogin}, {path:"/login",component:myLogin}, //注意冒号,不用/否则会当成地址 {path:"/main/:myName",component:myMain}, //没有匹配到任何页面则跳转到notfound页面 {path:"*",component:NotFound} ] const myRouter = new VueRouter({ routes:myRoutes }) new Vue({ router:myRouter, el:"#container", data:{ msg:"Hello VueJs" } }) // 注意,路由地址 </script> </body> </html>
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>传参练习</title> <script src="js/vue.js"></script> <script src="js/vue-router.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <!-- --> <router-view></router-view> </div> <script> //创建产品列表组件 var myList = Vue.component("product-list",{ //保存产品列表的数据 data:function(){ return{ productList:["苹果","华为","三星","小米","vivo"] } }, template:` <div> <h4>这是列表页</h4> <ul> <li v-for="(tmp,index) in productList"> //将index传递过去 <router-link v-bind:to="'/detail/'+index">{{tmp}}</router-link> </li> </ul> </div> ` }) //详情页组件 var myDetail = Vue.component("product-detail",{ //保存传递过来的index data:function(){ return{ myIndex:"" } }, //在挂载完成后,将接收到的index赋值给myIndex mounted:function(){ this.myIndex = this.$route.params.id; }, template:` <div> <h4>这是详情页</h4> <p>这是id为:{{myIndex}}的产品</p> </div> ` }) //页面找不到的时候 var NotFound = Vue.component("not-found",{ template:` <div> <h1>404 Page Not Found</h1> </div> ` }) // 配置路由词典 const myRoutes = [ {path:"",component:myList}, {path:"/list",component:myList}, {path:"/detail/:id",component:myDetail}, {path:"*",component:NotFound}, ] const myRouter = new VueRouter({ routes:myRoutes }) new Vue({ router:myRouter, el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
关于vue.js的学习教程,请大家点击专题vue.js组件学习教程、Vue.js前端组件学习教程进行学习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Vue-Router来实现组件间跳转的三种方法
- vue-router实现几级页面跳转及传参
- VueRouter&Bootstrap实现导航跳转
- vue-router 实现组件的缓存之 keep-alive
- vuejs 无node单页应用方案二(babel-standalone or traceur,vue-router实现、组件按需懒加载)
- vue-router登录校验后跳转到之前指定页面如何实现
- vue-router实现登录和跳转到指定页面,vue-router 传参
- Vue.js实战之利用vue-router实现跳转页面
- Vue 页面跳转不用router-link的实现代码
- vue-router 实现页面跳转
- Vue.js实战之利用vue-router实现跳转页面
- vue通过 vue.mixin(混合)实现 任意页面记录滚动条位置(路由router.query参数一致) 并跳转到上次的位置
- Vue组件模板形式实现对象数组数据循环为树形结构(实例代码)
- vue2.0 使用element-ui里的upload组件实现图片预览效果
- Vue实现PopupWindow组件详解
- 使用Vue的slot插槽分发父组件内容实现高度复用、更加灵活的组件
- vue刷新当前路由:router-view 复用组件时不刷新的3种解决方案总结
- vue组件实现Tab切换功能
- Vue-router结合transition实现app前进后退动画切换效果
- vue-router跳转页面