vue3父子组件传值

news/2025/3/25 10:04:59/

在 Vue 3 中,Composition API 是一种新的编写组件逻辑的方式,它通过 setup 函数提供了一种更灵活的方式来组织和复用代码。与传统的 Options API 相比,Composition API 更适合处理复杂的逻辑场景,尤其是在需要跨组件复用逻辑时。

以下是基于 Composition API 的父子组件传值的实现方式。


1. 父组件向子组件传递数据(Props)

示例代码

父组件(ParentComponent.vue)

javascript"><template><div><h1>父组件</h1><ChildComponent :message="parentMessage" /></div>
</template><script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';// 定义父组件的数据
const parentMessage = ref('Hello from Parent!');
</script>

子组件(ChildComponent.vue)

javascript"><template><div><h2>子组件</h2><p>从父组件接收到的消息:{{ message }}</p></div>
</template><script setup>
// 接收父组件传递的 props
defineProps({message: {type: String,required: true,},
});
</script>
解析
  1. 父组件通过 ref 定义响应式数据,并通过 v-bind 将其传递给子组件。
  2. 子组件使用 defineProps 来接收父组件传递的 props

2. 子组件向父组件传递数据(Events)

示例代码

父组件(ParentComponent.vue)

javascript"><template><div><h1>父组件</h1><p>从子组件接收到的消息:{{ childMessage }}</p><ChildComponent @send-message="receiveMessage" /></div>
</template><script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';// 定义父组件的数据
const childMessage = ref('');// 接收子组件传递的数据
const receiveMessage = (message) => {childMessage.value = message;
};
</script>

子组件(ChildComponent.vue)

javascript"><template><div><h2>子组件</h2><button @click="sendMessageToParent">发送消息给父组件</button></div>
</template><script setup>
import { defineEmits } from 'vue';// 定义触发事件的方法
const emit = defineEmits(['send-message']);const sendMessageToParent = () => {const message = 'Hello from Child!';emit('send-message', message); // 触发事件并传递数据
};
</script>
解析
  1. 子组件通过 defineEmits 定义可以触发的事件。
  2. 使用 emit 方法触发事件,并将数据传递给父组件。
  3. 父组件通过监听子组件的事件来接收数据。

3. 双向绑定(v-model)

示例代码

父组件(ParentComponent.vue)

javascript"><template><div><h1>父组件</h1><p>当前值:{{ inputValue }}</p><ChildComponent v-model="inputValue" /></div>
</template><script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';// 定义父组件的数据
const inputValue = ref('');
</script>

子组件(ChildComponent.vue)

javascript"><template><div><h2>子组件</h2><input :value="modelValue" @input="updateValue" /></div>
</template><script setup>
import { defineProps, defineEmits } from 'vue';// 接收父组件传递的 props
const props = defineProps({modelValue: {type: String,required: true,},
});// 定义触发事件的方法
const emit = defineEmits(['update:modelValue']);// 更新父组件的值
const updateValue = (event) => {emit('update:modelValue', event.target.value);
};
</script>
解析
  1. 父组件通过 v-modelinputValue 绑定到子组件。
  2. 子组件通过 defineProps 接收 modelValue,并通过 $emit('update:modelValue', value) 更新父组件的值。

4. 跨层级组件传值(Provide/Inject)

示例代码

祖父组件(GrandParentComponent.vue)

javascript"><template><div><h1>祖父组件</h1><ParentComponent /></div>
</template><script setup>
import { provide, ref } from 'vue';
import ParentComponent from './ParentComponent.vue';// 提供共享数据
const sharedMessage = ref('Hello from GrandParent!');
provide('sharedMessage', sharedMessage);
</script>

父组件(ParentComponent.vue)

javascript"><template><div><h2>父组件</h2><ChildComponent /></div>
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件(ChildComponent.vue)

javascript"><template><div><h3>子组件</h3><p>从祖父组件接收到的消息:{{ sharedMessage }}</p></div>
</template><script setup>
import { inject } from 'vue';// 注入共享数据
const sharedMessage = inject('sharedMessage');
</script>
解析
  1. 祖父组件通过 provide 提供数据。
  2. 子组件通过 inject 接收数据,无需经过中间层级。

总结

  • 父传子 :使用 defineProps
  • 子传父 :使用 defineEmitsemit
  • 双向绑定 :使用 v-modelupdate:modelValue
  • 跨层级传值 :使用 provideinject

Composition API 提供了更灵活和强大的方式来组织组件逻辑,特别是在复杂场景下。如果还有其他问题或需要更详细的示例,请随时补充说明!


http://www.ppmy.cn/news/1581253.html

相关文章

如何理解std::promise和std::future

std::promise 是 C11 引入的一个类&#xff0c;用于在线程之间传递异步结果&#xff08;值或异常&#xff09;。它通常与 std::future 配合使用&#xff0c;std::promise 用于设置值或异常&#xff0c;而 std::future 用于获取这些值或异常。 下面通过一个更直观的生产者-消费者…

红数码影视(RED Digital Cinema)存储卡格式化后的恢复方法

红数码影视(RED Digital Cinema)的摄像机可以生成两种RAW级高清视频文件&#xff0c;一种是R3D&#xff0c;一种是MOV。其中MOV属于苹果(apple)公司的QT视频封装结构&#xff0c;使用的视频编码是Apple ProRes;而R3D则是RED公司自创的RAW视频文件&#xff0c;这种文件解码需要使…

Excel(进阶篇):powerquery详解、PowerQuery的各种用法,逆透视表格、双行表头如何制作透视表、不规则数据如何制作数据透视表

目录 PowerQuery工具基础修改现有数据理规则PowerQuery抓取数据的两种方式多文件合并透视不同表结构多表追加数据透视追加与合并整理横向表格:逆透视 数据用拆分工具整理数据算账龄 不等步长值组合合并文件夹中所有文件PowerQuery处理CSV文件双行表头、带合并单元格如何做数据…

在 Spring Boot 结合 MyBatis 的项目中,实现字段脱敏(如手机号、身份证号、银行卡号等敏感信息的部分隐藏)可以通过以下方案实现

在 Spring Boot 结合 MyBatis 的项目中&#xff0c;实现字段脱敏&#xff08;如手机号、身份证号、银行卡号等敏感信息的部分隐藏&#xff09;可以通过以下方案实现。以下是分步说明和完整代码示例&#xff1a; 一、实现方案选择 1. 方案一&#xff1a;自定义注解 Jackson 序…

迷你主机与普通台式电脑区别

一、【两者区别】 1、迷你电脑主机大部分都是采用带U的芯片&#xff0c;也就是低功耗芯片&#xff0c;整机功耗在10W-25W左右&#xff1b;普通台式机自然就是用台式机芯片&#xff0c;整机功耗约150W-300W&#xff1b; 2、迷你电脑主机大部分采用低压内存&#xff0c;也就是笔…

MSE分类时梯度消失的问题详解和交叉熵损失的梯度推导

下面是MSE不适合分类任务的解释&#xff0c;包含梯度推导。以及交叉熵的梯度推导。 前文请移步笔者的另一篇博客&#xff1a;大模型训练为什么选择交叉熵损失&#xff08;Cross-Entropy Loss&#xff09;&#xff1a;均方误差&#xff08;MSE&#xff09;和交叉熵损失的深入对比…

简述一下Unity中的碰撞检测

碰撞检测的基本工作原理 Unity中的碰撞检测是游戏开发中的核心功能&#xff0c;依赖于NVIDIA的PhysX物理引擎实现。它通过碰撞体&#xff08;Collider&#xff09;和刚体&#xff08;Rigidbody&#xff09;两个主要组件来检测和响应游戏对象之间的物理交互&#xff0c;遵循以下…

如何用Deepseek制作流程图?

使用Deepseek制作流程图&#xff0c;本质上是让AI根据你的需求&#xff0c;生成相关流程图的代码&#xff0c;然后在流程图编辑器中渲染&#xff0c;类似于Python一样&#xff0c;ChatGPT可以生成代码&#xff0c;但仍需在IDE中执行。 你知道绘制流程图最高效的工具是什么吗&a…