Vue 3 中,组件间传值有多种方式

在 Vue 3 中,组件间传值有多种方式,以下是几种常见的方式

父组件向子组件传值(通过 props):

javascript 复制代码
<!-- 父组件 -->
<template>
  <ChildComponent :message="parentMessage" />
</template>

<script setup>
import ChildComponent from './ChildComponent.vue';
const parentMessage = '这是来自父组件的值';
</script>

<!-- 子组件 -->
<template>
  <div>{{ message }}</div>
</template>

<script setup>
defineProps(['message']);
</script>

以下是几个父组件向子组件传值的示例:

示例 1:传递字符串

父组件:

javascript 复制代码
<template>
  <ChildComponent :message="messageFromParent" />
</template>

<script setup>
import ChildComponent from './ChildComponent.vue';
const messageFromParent = '这是来自父组件的消息';
</script>

子组件:

javascript 复制代码
<template>
  <div>{{ message }}</div>
</template>

<script setup>
defineProps(['message']);
</script>
示例 2:传递数字

父组件:

javascript 复制代码
<template>
  <ChildComponent :count="5" />
</template>

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

子组件:

javascript 复制代码
<template>
  <div>接收到的数字: {{ count }}</div>
</template>

<script setup>
defineProps(['count']);
</script>
示例 3:传递对象

父组件:

javascript 复制代码
<template>
  <ChildComponent :userInfo="{ name: '张三', age: 25 }" />
</template>

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

子组件:

javascript 复制代码
<template>
  <div>用户信息: {{ userInfo.name }}, {{ userInfo.age }}</div>
</template>

<script setup>
defineProps(['userInfo']);
</script>
示例 4:传递数组

父组件:

javascript 复制代码
<template>
  <ChildComponent :fruits="['苹果', '香蕉', '橙子']" />
</template>

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

子组件:

javascript 复制代码
<template>
  <div>水果列表: 
    <ul>
      <li v-for="fruit in fruits" :key="fruit">{{ fruit }}</li>
    </ul>
  </div>
</template>

<script setup>
defineProps(['fruits']);
</script>
示例 5:传递布尔值

父组件:

javascript 复制代码
<template>
  <ChildComponent :isActive="true" />
</template>

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

子组件:

javascript 复制代码
<template>
  <div v-if="isActive">当前状态为活跃</div>
  <div v-else>当前状态为不活跃</div>
</template>

<script setup>
defineProps(['isActive']);
</script>

子组件向父组件传值(通过自定义事件):

javascript 复制代码
<!-- 父组件 -->
<template>
  <ChildComponent @childEvent="handleChildEvent" />
</template>

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

function handleChildEvent(valueFromChild) {
  console.log('从子组件接收到的值:', valueFromChild);
}
</script>

<!-- 子组件 -->
<template>
  <button @click="emitEvent">向父组件传值</button>
</template>

<script setup>
defineEmits(['childEvent']);

function emitEvent() {
  const valueToSend = '这是来自子组件的值';
  emit('childEvent', valueToSend);
}
</script>

子组件使用 v-model 向父组件传值的示例代码:

父组件:

html

复制

父组件接收到的值: {{ inputValue }}

子组件:

html

复制

<input type="text" :value="value" @input="emit('update:value', event.target.value)" />

在上述示例中,父组件通过 v-model:value="inputValue" 将 inputValue 与子组件进行绑定。子组件中的输入框的值通过 :value="value" 与父组件传来的值进行关联,当输入框的值发生变化时,通过 @input="emit('update:value', event.target.value)" 触发 update:value 事件并将新的值传递给父组件,从而实现子组件向父组件传值。

通过 provide 和 inject:

javascript 复制代码
<!-- 父组件 -->
<template>
  <ChildComponent />
</template>

<script setup>
import { provide } from 'vue';

provide('sharedValue', '这是共享的值');
</script>

<!-- 子组件 -->
<template>
  <div>{{ sharedValue }}</div>
</template>

<script setup>
import { inject } from 'vue';

const sharedValue = inject('sharedValue');
</script>

使用 Vuex 状态管理:

首先安装 vuex :npm install vuex@next --save

创建一个 store.js 文件:

javascript 复制代码
import { createStore } from 'vuex';

export default createStore({
  state: {
    commonValue: '这是全局的值',
  },
  mutations: {
    updateCommonValue(state, newValue) {
      state.commonValue = newValue;
    }
  },
  actions: {},
  getters: {}
});

在父组件和子组件中使用:

javascript 复制代码
<!-- 父组件或子组件 -->
<template>
  <div>{{ $store.state.commonValue }}</div>
</template>

<script setup>
import { useStore } from 'vuex';

const store = useStore();

function updateValue() {
  store.commit('updateCommonValue', '新的值');
}
</script>

这些是 Vue 3 中常见的组件间传值方式,您可以根据具体的项目需求选择合适的方法。

相关推荐
前端不太难7 小时前
从 Navigation State 反推架构腐化
前端·架构·react
前端程序猿之路7 小时前
Next.js 入门指南 - 从 Vue 角度的理解
前端·vue.js·语言模型·ai编程·入门·next.js·deepseek
大布布将军7 小时前
⚡️ 深入数据之海:SQL 基础与 ORM 的应用
前端·数据库·经验分享·sql·程序人生·面试·改行学it
川贝枇杷膏cbppg8 小时前
Redis 的 RDB 持久化
前端·redis·bootstrap
D_C_tyu8 小时前
Vue3 + Element Plus | el-table 表格获取排序后的数据
javascript·vue.js·elementui
JIngJaneIL8 小时前
基于java+ vue农产投入线上管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
天外天-亮8 小时前
v-if、v-show、display: none、visibility: hidden区别
前端·javascript·html
jump_jump8 小时前
手写一个 Askama 模板压缩工具
前端·性能优化·rust
hellotutu9 小时前
vue2 从 sessionStorage 手动取 token 后,手动加入到 header
vue.js·token·session·header
be or not to be9 小时前
HTML入门系列:从图片到表单,再到音视频的完整实践
前端·html·音视频