这次给大家带来vue-router组件间参数相互传递的方法,vue-router组件参数相互传递的注意事项有哪些,下面就是实战案例,一起来看一下。
通过VueRouter来实现组件之间的跳转:参数的传递,具体内容如下
login —用户名–>main
①明确发送方和接收方
②配置接收方的路由地址
{path:’/myTest’,component:TestComponent}
–>
{path:’/myTest/:id’,component:TestComponent}
③接收方获取传递来的数据
this.$route.params.id
④跳转的时候,发送参数
this.$router.push(‘/myTest/20’)
跳转
代码:
传参
{{msg}}
//创建主页面组件 var myMain = Vue.component("main-component",{ //保存登录传递过来的数据 data:function(){ return { uName:'' } }, template:`
主页面用户名:{{uName}}
`, //挂载该组件时自动拿到数据 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:`
登录页面
登录到主页面 ` }) var NotFound = Vue.component("not-found",{ template:`
404 Page Not Found
返回登录页 ` }) //配置路由词典 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" } })// 注意,路由地址
登录后复制
传参练习
{{msg}}
//创建产品列表组件 var myList = Vue.component("product-list",{ //保存产品列表的数据 data:function(){ return{ productList:["苹果","华为","三星","小米","vivo"] } }, template:`
这是列表页
- //将index传递过去 {{tmp}}
这是详情页
这是id为:{{myIndex}}的产品
` })//页面找不到的时候 var NotFound = Vue.component("not-found",{ template:`
404 Page Not Found
` })// 配置路由词典 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" } })登录后复制
相信看了本文案例你已经掌握了方法,更多精彩请关注【创想鸟】其它相关文章!
推荐阅读:
立即学习“前端免费学习笔记(深入)”;
mint-ui loadmore上拉加载与下拉刷新冲突处理方法
微信小程序怎样使图片上传至服务器
以上就是vue-router组件间参数相互传递的方法的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2772125.html