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

vue3父子组件传值

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

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


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

示例代码

父组件(ParentComponent.vue)

<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)

<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)

<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)

<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)

<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)

<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)

<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)

<template>
  <div>
    <h2>父组件</h2>
    <ChildComponent />
  </div>
</template>

<script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件(ChildComponent.vue)

<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.kler.cn/a/600259.html

相关文章:

  • JavaScript 事件循环
  • JVM中的运行时常量池详解
  • 【QA】Qt有哪些迭代器模式的应用?
  • Vue中的状态管理器Vuex被Pinia所替代-上手使用指南
  • 买卖股票的最佳时机(js实现,LeetCode:121)
  • 【Excel使用技巧】某列保留固定字段或内容
  • 多语言语料库万卷·丝路2.0开源,数据模态全面升级,搭建文化交流互鉴AI桥梁
  • 原子化 CSS
  • 护网面试题总结
  • Java 集合 List、Set、Map 区别与应用
  • 基于Spring Boot + Vue的银行管理系统设计与实现
  • XDP/eBPF来包过滤-已上机验证
  • CSS实现当鼠标悬停在一个元素上时,另一个元素的样式发生变化的效果
  • 《AI Agent智能应用从0到1定制开发》学习笔记:使用RAG技术增强大模型能力,实现与各种文档的对话
  • CSS语言的双向链表
  • 网络运维学习笔记(DeepSeek优化版) 020 HCIA-Datacom新增知识点02 SDN与NFV概述
  • 6(六)Jmeter线程数分段加压
  • 基于Linux的多进程并发服务器设计与实现
  • RISC-V AIA学习2---IMSIC
  • docker pull时报错:https://registry-1.docker.io/v2/