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

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

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

    • 父组件向子组件传值(通过 props):
      • 以下是几个父组件向子组件传值的示例:
        • 示例 1:传递字符串
        • 示例 2:传递数字
        • 示例 3:传递对象
        • 示例 4:传递数组
        • 示例 5:传递布尔值
    • 子组件向父组件传值(通过自定义事件):
      • 子组件使用 v-model 向父组件传值的示例代码:
    • 通过 provide 和 inject:
    • 在父组件和子组件中使用:

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

<!-- 父组件 -->
<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:传递字符串

父组件:

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

子组件:

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

父组件:

<template><ChildComponent :count="5" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件:

<template><div>接收到的数字: {{ count }}</div>
</template><script setup>
defineProps(['count']);
</script>
示例 3:传递对象

父组件:

<template><ChildComponent :userInfo="{ name: '张三', age: 25 }" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件:

<template><div>用户信息: {{ userInfo.name }}, {{ userInfo.age }}</div>
</template><script setup>
defineProps(['userInfo']);
</script>
示例 4:传递数组

父组件:

<template><ChildComponent :fruits="['苹果', '香蕉', '橙子']" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件:

<template><div>水果列表: <ul><li v-for="fruit in fruits" :key="fruit">{{ fruit }}</li></ul></div>
</template><script setup>
defineProps(['fruits']);
</script>
示例 5:传递布尔值

父组件:

<template><ChildComponent :isActive="true" />
</template><script setup>
import ChildComponent from './ChildComponent.vue';
</script>

子组件:

<template><div v-if="isActive">当前状态为活跃</div><div v-else>当前状态为不活跃</div>
</template><script setup>
defineProps(['isActive']);
</script>

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

<!-- 父组件 -->
<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:

<!-- 父组件 -->
<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 文件:

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

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

<!-- 父组件或子组件 -->
<template><div>{{ $store.state.commonValue }}</div>
</template><script setup>
import { useStore } from 'vuex';const store = useStore();function updateValue() {store.commit('updateCommonValue', '新的值');
}
</script>

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

http://www.lryc.cn/news/422765.html

相关文章:

  • 前置(3):npm 和npx异同点
  • 笔记(day17)集合概述、List、Set、比较器
  • C语言从头学45——I/O函数(二)
  • Python爬虫——爬取bilibili中的视频
  • 为什么企业电销要用外呼系统
  • Keepalived + Nginx 主备容灾方案介绍
  • PHP、JavaScript代码审计工具
  • 《向量数据库指南》——Ray Data+Anyscale解锁价值评估新篇章
  • 知识改变命运 数据结构【杨辉三角(顺序表)】
  • Docker三剑客之Docker Engine
  • 【Qt】信号与槽(下)
  • 多模态大语言模型(MMLLM)的现状、发展和潜力
  • Linux中apache服务安装与mysql安装
  • Sublime Text常用快捷键
  • 高危漏洞CVE-2024-38077的修复指南
  • docker基本管理和应用
  • AI招聘在人才盘活中的作用:开启智慧人力新篇章
  • 探索SD NAND配套测试工具:工程师的得力助手
  • 三十六、【人工智能】【机器学习】【监督学习】- Bagging算法模型
  • 2024年8月8日(python基础)
  • SpringAOP_面向切面编程
  • 芯片bring-up的测试用例
  • vs code编辑区域右键菜单突然变短
  • 如何将TRIZ的“最终理想解”应用到机器人电机控制设计中?
  • 【记录】基于docker部署小熊派BearPi-Pico H3863开发环境
  • Elasticsearch 与 OpenSearch:谁才是搜索霸主
  • WEB渗透-TomcatAjp之LFIRCE
  • 嵌入式初学-C语言-二一
  • 2376. 统计特殊整数
  • Python 绘图进阶之核密度估计图:掌握数据分布的秘密