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

Vue3 组件之间传值

在 Vue3 中,组件之间的数据传递主要有以下几种方式,适用于不同的场景:


一、父组件向子组件传值:props

1. 子组件定义 props
<!-- ChildComponent.vue -->
<script setup>
// 组合式 API(推荐)
const props = defineProps({title: {type: String,default: '默认标题'},count: {type: Number,required: true}
});
</script><template><div>{{ title }} - {{ count }}</div>
</template>
<!-- 选项式 API -->
<script>
export default {props: {title: String,count: {type: Number,required: true}}
}
</script>
2. 父组件传递数据
<template><ChildComponent :title="'Hello Vue3'" :count="42"/>
</template>

二、子组件向父组件传值:emit 事件

1. 子组件触发事件
<!-- ChildComponent.vue -->
<script setup>
const emit = defineEmits(['updateCount']); // 定义事件function increment() {emit('updateCount', 10); // 触发事件并传递数据
}
</script><template><button @click="increment">增加计数</button>
</template>
2. 父组件监听事件
<template><ChildComponent :count="count"@updateCount="count = $event" <!-- 接收子组件传递的值 -->/>
</template><script setup>
import { ref } from 'vue';
const count = ref(0);
</script>

三、双向绑定:v-model

1. 子组件支持 v-model
<!-- ChildComponent.vue -->
<script setup>
const props = defineProps(['modelValue']);
const emit = defineEmits(['update:modelValue']);function updateValue(newValue) {emit('update:modelValue', newValue);
}
</script><template><input :value="modelValue" @input="updateValue($event.target.value)">
</template>
2. 父组件使用 v-model
<template><ChildComponent v-model="text" /> <!-- 自动同步数据 -->
</template><script setup>
import { ref } from 'vue';
const text = ref('初始值');
</script>

四、跨层级传值:provide / inject

1. 祖先组件提供数据
<!-- AncestorComponent.vue -->
<script setup>
import { provide, ref } from 'vue';const theme = ref('dark');
provide('theme', theme); // 提供数据
</script>
2. 后代组件注入数据
<!-- ChildComponent.vue -->
<script setup>
import { inject } from 'vue';const theme = inject('theme'); // 注入数据
</script><template><div :class="theme">当前主题:{{ theme }}</div>
</template>

五、通过 Context 共享数据(如 Pinia 状态管理)

1. 安装 Pinia
npm install pinia
2. 创建 Store
// stores/counter.js
import { defineStore } from 'pinia';export const useCounterStore = defineStore('counter', {state: () => ({count: 0}),actions: {increment() {this.count++;}}
});
3. 组件中使用 Store
<template><div>Count: {{ counterStore.count }}</div><button @click="counterStore.increment()">+1</button>
</template><script setup>
import { useCounterStore } from '@/stores/counter';const counterStore = useCounterStore();
</script>

总结:不同场景的传值方式

场景方法适用性
父子组件直接通信props + emit父子层级明确,数据流单向
表单输入双向绑定v-model表单类组件(如输入框、下拉框)
跨层级组件通信provide / inject深层次嵌套组件(如全局配置、主题)
复杂应用状态共享Pinia / Vuex多组件共享状态(推荐中大型项目)

推荐实践

  • 优先使用 propsemit 实现父子通信。
  • 简单双向绑定用 v-model,复杂逻辑用事件。
  • 跨层级或全局状态使用 Pinia。
  • 避免过度依赖 provide/inject,可能导致组件耦合。

相关文章:

  • elementui初学1
  • 第15天-NumPy科学计算实战:从基础到图像处理
  • 是德科技 | 单通道448G未来之路:PAM4? PAM6? PAM8?
  • 给几张图片和一段文字,怎么制作成带有语音的视频---php
  • FPGA降低功耗研究
  • spring cloud config更新配置
  • 【VLNs篇】02:NavGPT-在视觉与语言导航中使用大型语言模型进行显式推理
  • SAP-ABAP:ABAP异常处理与SAP现代技术融合—— 面向云原生、微服务与低代码场景的创新实践
  • digitalworld.local: FALL靶场
  • 无人机桥梁巡检
  • eclipse 生成函数说明注释
  • Hadoop-HA高可用集群启动nameNode莫名挂掉,排错解决
  • 固定翼无人机抛投技术分析!
  • 计算机网络相关面试题
  • 自定义协议与序列反序列化
  • Halcon数据类型
  • File文件
  • Python实现蛋白质结构RMSD计算
  • Hass-Panel - 开源智能家居控制面板
  • JavaEE 初阶文件操作与 IO 详解
  • 网站制作可以/推广之家
  • 沧州做网站/网络营销好找工作吗
  • 做网站优化价格/长沙网站推广排名
  • 永嘉营销网站建设/成都网站快速优化排名
  • 网站建设方案书 下载/东莞关键词优化推广
  • 动态网站开发加载图片/搜索引擎优化的流程