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

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.dtcms.com/a/189163.html

相关文章:

  • Linux系统编程---Signal信号集
  • Datawhale PyPOTS时间序列5月第1次笔记
  • 【速写】TRL:Trainer的细节与思考(PPO/DPO+LoRA可行性)
  • JavaWeb 开发的核心基础知识
  • 2025-05-13 学习记录--Python-条件判断:if语句 + if-else语句 + if-elif-else语句 + match语句
  • 码蹄集——分解、数组最大公约数、孪生质数、卡罗尔数、阶乘数
  • The Deep Learning Compiler: A Comprehensive Survey (深度学习编译器:全面调查)
  • py7zr解压文件时报错CrcError(crc32, f.crc32, f.filename)
  • Kubernetes 入门笔记
  • 磁盘存储链式的 B 树与 B+ 树
  • 数据库原理实验:视图与索引
  • Python训练打卡Day23
  • Biba安全模型详解:守护信息系统完整性的基石
  • 网络安全-等级保护(等保) 2-3 GB/T 22240—2020《信息安全技术 网络安全等级保护定级指南》-2020-04-28发布【现行】
  • JAVA:synchronized总结
  • 大模型核心运行机制
  • C语言中的宏
  • Prometheus参数配置最佳实践
  • P1032 [NOIP 2002 提高组] 字串变换
  • shell脚本变量详解
  • 【WebApi】YiFeiWebApi接口安装说明
  • python: union()函数用法
  • uniapp+vue3开发项目之引入vuex状态管理工具
  • 内存泄漏系列专题分析之十三:高通相机CamX内存泄漏内存占用分析--Camx pipeline的ION内存拆解方法
  • 从 Vue3 回望 Vue2:响应式的内核革命
  • 【bag of n-grams】 N-gram词袋模型 简介
  • 已情感分析入门学习大模型-初级篇
  • 进程与线程:09 进程同步与信号量
  • QLineEdit增加点击回显功能
  • Python 字典键 “三变一” 之谜