您现在的位置是:网站首页> 编程资料编程资料
vue-router传参的4种方式超详细讲解_vue.js_
2023-05-24
184人已围观
简介 vue-router传参的4种方式超详细讲解_vue.js_
一、router-link路由导航方式传参
父组件:
子组件:this.$route.params.content接受父组件传递过来的参数
例如:
路由配置:
bashbash{path:'/father/son/:num',name:A,component:A}``` 地址栏中的显示:
http://localhost:8080/#/father/son/44
调用方法:
父亲组件 子组件通过 this.$route.params.num 接受参数
二、调用$router.push实现路由传参
父组件:通过实践触发,跳转代码
methods: { clickHand(id) { this.$router.push({ path: `/d/${id}` }) } } 路由配置
{path: '/d/:id', name: D, component: D} 地址栏中显示:
http://localhost:8080/d/123
子组件接受参数方式
mounted () { this.id = this.$route.params.id } 三、通过路由属性name匹配路由,再根据params传递参数
父组件:
ClickByName() { this.$router.push({ name: 'B', params: { context: '吴又可吴又可吴又可' } }) } 路由配置:路径后不需要在加上传入的参数,但是name必须和父组件中的name一致
{path: '/b', name: 'B', component: B} 地址栏中的显示:地址栏不会带有传入的参数,而且再次刷新页面后参数会丢失
http://localhost:8080/#/b
子组件接收参数的方式:
This is page B!传入参数:{{this.$route.params.context}}
四、通过query来传递参数
父组件:
clickQuery() { this.$router.push({ path: '/c', query: { context: '吴又可吴又可' } }) }路由配置:不需要做任何修改
{path: '/c', name: 'C', component: C} 地址栏中的显示(中文转码格式):
http://localhost:8080/#/c?sometext=%E8%BF%99%E6%98%AF%E5%B0%8F%E7%BE%8A%E5%90%8C%E5%AD%A6
子组件接受方法:
This is page C!这是父组件传入的数据: {{this.$route.query.context}}
工作中经常用的也就是上面的几种传参方式,完结~
总结
到此这篇关于vue-router传参的4种方式的文章就介绍到这了,更多相关vue-router传参方式内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关内容
- vue-json-editor json编辑器的使用_vue.js_
- Vue调试工具vue-devtools的安装与使用_vue.js_
- VUE使用vue create命令创建vue2.0项目的全过程_vue.js_
- npm install编译时报"Cannot read properties of null (reading ‘pickAlgorithm‘)"错误的解决办法_node.js_
- 解决threeJS加载obj gltf模型后颜色太暗的方法_vue.js_
- JS前端监控采集用户行为的N种姿势_JavaScript_
- JavaScript作用域与作用域链优化方式_javascript技巧_
- Vue项目中接口调用的详细讲解_vue.js_
- react render props模式实现组件复用示例_React_
- 基于JS制作一个网页版的猜数字小游戏_javascript技巧_
