Vue3 组件之间传值

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


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

1. 子组件定义 props
vue 复制代码
<!-- ChildComponent.vue -->
<script setup>
// 组合式 API(推荐)
const props = defineProps({
  title: {
    type: String,
    default: '默认标题'
  },
  count: {
    type: Number,
    required: true
  }
});
</script>

<template>
  <div>{{ title }} - {{ count }}</div>
</template>
vue 复制代码
<!-- 选项式 API -->
<script>
export default {
  props: {
    title: String,
    count: {
      type: Number,
      required: true
    }
  }
}
</script>
2. 父组件传递数据
vue 复制代码
<template>
  <ChildComponent 
    :title="'Hello Vue3'" 
    :count="42"
  />
</template>

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

1. 子组件触发事件
vue 复制代码
<!-- ChildComponent.vue -->
<script setup>
const emit = defineEmits(['updateCount']); // 定义事件

function increment() {
  emit('updateCount', 10); // 触发事件并传递数据
}
</script>

<template>
  <button @click="increment">增加计数</button>
</template>
2. 父组件监听事件
vue 复制代码
<template>
  <ChildComponent 
    :count="count"
    @updateCount="count = $event" <!-- 接收子组件传递的值 -->
  />
</template>

<script setup>
import { ref } from 'vue';
const count = ref(0);
</script>

三、双向绑定:v-model

1. 子组件支持 v-model
vue 复制代码
<!-- 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
vue 复制代码
<template>
  <ChildComponent v-model="text" /> <!-- 自动同步数据 -->
</template>

<script setup>
import { ref } from 'vue';
const text = ref('初始值');
</script>

四、跨层级传值:provide / inject

1. 祖先组件提供数据
vue 复制代码
<!-- AncestorComponent.vue -->
<script setup>
import { provide, ref } from 'vue';

const theme = ref('dark');
provide('theme', theme); // 提供数据
</script>
2. 后代组件注入数据
vue 复制代码
<!-- ChildComponent.vue -->
<script setup>
import { inject } from 'vue';

const theme = inject('theme'); // 注入数据
</script>

<template>
  <div :class="theme">当前主题:{{ theme }}</div>
</template>

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

1. 安装 Pinia
bash 复制代码
npm install pinia
2. 创建 Store
javascript 复制代码
// stores/counter.js
import { defineStore } from 'pinia';

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  actions: {
    increment() {
      this.count++;
    }
  }
});
3. 组件中使用 Store
vue 复制代码
<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,可能导致组件耦合。
相关推荐
人工智能训练5 小时前
【极速部署】Ubuntu24.04+CUDA13.0 玩转 VLLM 0.15.0:预编译 Wheel 包 GPU 版安装全攻略
运维·前端·人工智能·python·ai编程·cuda·vllm
会跑的葫芦怪5 小时前
若依Vue 项目多子路径配置
前端·javascript·vue.js
xiaoqi9226 小时前
React Native鸿蒙跨平台如何进行狗狗领养中心,实现基于唯一标识的事件透传方式是移动端列表开发的通用规范
javascript·react native·react.js·ecmascript·harmonyos
jin1233227 小时前
React Native鸿蒙跨平台剧本杀组队消息与快捷入口组件,包含消息列表展示、快捷入口管理、快捷操作触发和消息详情预览四大核心功能
javascript·react native·react.js·ecmascript·harmonyos
烬头88218 小时前
React Native鸿蒙跨平台实现二维码联系人APP(QRCodeContactApp)
javascript·react native·react.js·ecmascript·harmonyos
pas1368 小时前
40-mini-vue 实现三种联合类型
前端·javascript·vue.js
摇滚侠8 小时前
2 小时快速入门 ES6 基础视频教程
前端·ecmascript·es6
2601_949833398 小时前
flutter_for_openharmony口腔护理app实战+预约管理实现
android·javascript·flutter
珑墨9 小时前
【Turbo】使用介绍
前端
军军君019 小时前
Three.js基础功能学习十三:太阳系实例上
前端·javascript·vue.js·学习·3d·前端框架·three