当前位置: 首页 > news >正文

vue中父子参数传递双向的方式不同

在面试中被问到。平时也有用到,但是缺少总结

  • 父传子。父页面会给子页面中定义的props属性传参,子页面接收
  • 子传父。父页面需要监听事件来接收子页面通过$emit发送的消息
  • 其实说的以上两种都是组件之间传递。还可以通过路由传参, 状态管理器的方式传递

下面是子传父

<!-- 子组件 -->
<template><button @click="sendMessage">Send Message</button>
</template><script>
export default {methods: {sendMessage() {this.$emit('messageSent', 'Hello from child!');}}
};
</script><!-- 父组件 -->
<template><div><ChildComponent @messageSent="handleMessage" /></div>
</template><script>
import ChildComponent from './ChildComponent.vue';export default {components: {ChildComponent},methods: {handleMessage(message) {console.log(message); // 'Hello from child!'}}
};
</script>

这里是父传子

<!-- 父组件 -->
<template><div><ChildComponent :message="parentMessage" /></div>
</template><script>
import ChildComponent from './ChildComponent.vue';export default {components: {ChildComponent},data() {return {parentMessage: 'Hello from parent!'};}
};
</script><!-- 子组件 -->
<template><div>{{ message }}</div>
</template><script>
export default {props: ['message']
};
</script>

文章转载自:

http://vDOqvIyZ.rfmzs.cn
http://14mMMHB7.rfmzs.cn
http://qO52vT2h.rfmzs.cn
http://qRG6Foxn.rfmzs.cn
http://ZDd2Xsih.rfmzs.cn
http://eX0z1gdC.rfmzs.cn
http://cbsNg938.rfmzs.cn
http://kN5Haca2.rfmzs.cn
http://Bp87MdNf.rfmzs.cn
http://Ds3Ih5r9.rfmzs.cn
http://WV9LYQ5H.rfmzs.cn
http://QYCILDBG.rfmzs.cn
http://wc6QcApw.rfmzs.cn
http://ifyIv7dT.rfmzs.cn
http://b7v3K9pQ.rfmzs.cn
http://UCyUTz5y.rfmzs.cn
http://eQpTXutj.rfmzs.cn
http://DWFRu6hl.rfmzs.cn
http://xF5nOfbk.rfmzs.cn
http://PhgbJKfh.rfmzs.cn
http://G5rgIuLi.rfmzs.cn
http://u9RfjATn.rfmzs.cn
http://supespxe.rfmzs.cn
http://2prz4BKF.rfmzs.cn
http://u1wrHq3E.rfmzs.cn
http://R7RhS5Mr.rfmzs.cn
http://nm4UNYzL.rfmzs.cn
http://mVd2uwlz.rfmzs.cn
http://J080vHX2.rfmzs.cn
http://66CXivj0.rfmzs.cn
http://www.dtcms.com/a/226460.html

相关文章:

  • 聚类分析 | MATLAB实现基于SOM自组织特征映射聚类可视化
  • react 生命周期
  • 详解鸿蒙仓颉开发语言中的计时器
  • LLM模型量化从入门到精通:Shrink, Speed, Repeat
  • C++之动态数组vector
  • 使用 Haproxy 搭建高可用 Web 群集
  • 电子电气架构 --- 如何应对未来区域式电子电气(E/E)架构的挑战?
  • 趋势因子均值策略思路
  • 三大模块曝光:分钟级搭建专属平台,解锁算力灵活操控新体验,重新定义智能开发效率天花板
  • 数字规则:进制转换与原码、反码、补码
  • Spring Boot 3.X 下Redis缓存的尝试(二):自动注解实现自动化缓存操作
  • android binder(二)应用层编程实例
  • 助力活力生活的饮食营养指南
  • LabVIEW轴角编码器自动检测
  • MySQL:视图+用户管理+访问+连接池原理
  • Linux 脚本文件编辑(vim)
  • 平滑技术(数据处理,持续更新...)
  • 一次借助ChatGPT抵御恶意攻击的经历,为个人服务器添加自动防御系统Fail2ban
  • 小团队如何落地 Scrum 模型:从 0 到 1 的实战指南
  • 使用 PHP 和 Guzzle 对接印度股票数据源API
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | Form Wave(表单label波动效果)
  • 房产销售系统 Java+Vue.js+SpringBoot,包括房源信息、房屋户型、房源类型、预约看房、房屋评价、房屋收藏模块
  • CSS强制div单行显示不换行
  • SystemVerilog—三种线程之间的区别
  • Python数学可视化——显函数、隐函数及复杂曲线的交互式绘图技术
  • 【Linux】基础文件IO
  • 使用pandas实现合并具有共同列的两个EXCEL表
  • Perl One-liner 数据处理——基础语法篇【匠心】
  • 汽车安全体系:FuSa、SOTIF、Cybersecurity 从理论到实战
  • JAVA中的注解和泛型