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

vue3父子组件传值

父 → 子:props

父组件

<template><ChildComponent :message="parentMessage" :user="user" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
const parentMessage = 'Hello from parent';
const user = { name: 'Alice', age: 25 };
</script>

子组件

<template><p>{{ message }}</p><p>{{ user.name }} - {{ user.age }}</p>
</template><script setup>
// 声明 props
const props = defineProps({message: {type: String,required: true},user: {type: Object,required: true}
});
</script>

子 → 父:emit 事件

<template><button @click="sendToParent">Send to Parent</button>
</template><script setup>
const emit = defineEmits(['send-data']);function sendToParent() {emit('send-data', 'Data from child');
}
</script>

父组件

<template><ChildComponent @send-data="handleChildData" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';function handleChildData(data) {console.log('Received:', data); // 输出 "Received: Data from child"
}
</script>
http://www.xdnf.cn/news/5874.html

相关文章:

  • 数据治理域——日志数据采集设计
  • c++STL-list的模拟实现
  • conda 输出指定python环境的库 输出为 yaml文件
  • K230 ISP:一种新的白平衡标定方法
  • AcroForm 格式化文本(域)字段
  • ElasticSearch父子关系数据建模
  • MySQL命令行导出数据(docker版本)
  • 运行Spark程序-在shell中运行1
  • 智源联合南开大学开源Chinese-LiPS中文多模态语音识别数据集
  • base64形式的图片数据保存方法
  • Redis介绍与使用
  • 【git】clone项目后续,github clone的网络配置,大型项目git log 输出txt,切换commit学习,goland远程,自存档
  • 关于maven的依赖下不下来的问题
  • Git基本操作命令
  • 专题四:综合练习( 找出所有子集的异或总和再求和)
  • 解锁Python TDD:从理论到实战的高效编程之道(9/10)
  • 时间序列预测建模的完整流程以及数据分析【学习记录】
  • 选择单例还是依赖注入
  • 【每天一个知识点】Dip 检验(Dip test)
  • CSS经典布局之圣杯布局和双飞翼布局
  • spark-cache模式
  • ubuntu22.04编译PX4无人机仿真实践
  • EMQX v5.0通过连接器和规则同步数据
  • PyInstaller 打包后 Excel 转 CSV 报错解决方案:“excel file format cannot be determined“
  • 【LUT技术专题】SPFLUT代码解读
  • Mirror的多人连接管理及房间系统
  • github 上的 CI/CD 的尝试
  • 掌握Multi-Agent实践(五):基于KIMAs的多智能体知识集成系统构建与应用实践
  • 每日算法刷题计划Day5 5.13:leetcode数组3道题,用时1h
  • AFFS2 的 `yaffs_ext_tags` 数据结构详解