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

Vue 3 中父组件内两个子组件相互传参的几种方法

方法一:通过父组件中转(Props + Emits)

<!-- ParentComponent.vue -->
<template><ChildA :message-from-b="messageFromB" @send-to-b="handleSendToB" /><ChildB :message-from-a="messageFromA" @send-to-a="handleSendToA" />
</template><script setup>
import { ref } from 'vue'
import ChildA from './ChildA.vue'
import ChildB from './ChildB.vue'const messageFromA = ref('')
const messageFromB = ref('')const handleSendToA = (message) => {messageFromA.value = message
}const handleSendToB = (message) => {messageFromB.value = message
}
</script><!-- ChildA.vue -->
<template><div><p>收到B的消息: {{ messageFromB }}</p><button @click="sendMessage">发送消息给B</button></div>
</template><script setup>
defineProps(['messageFromB'])
const emit = defineEmits(['send-to-b'])const sendMessage = () => {emit('send-to-b', '这是来自A的消息')
}
</script><!-- ChildB.vue -->
<template><div><p>收到A的消息: {{ messageFromA }}</p><button @click="sendMessage">发送消息给A</button></div>
</template><script setup>
defineProps(['messageFromA'])
const emit = defineEmits(['send-to-a'])const sendMessage = () => {emit('send-to-a', '这是来自B的消息')
}
</script>

方法二:使用 Provide/Inject

<!-- ParentComponent.vue -->
<template><ChildA /><ChildB />
</template><script setup>
import { provide, ref } from 'vue'
import ChildA from './ChildA.vue'
import ChildB from './ChildB.vue'const sharedState = ref({messageFromA: '',messageFromB: ''
})provide('sharedState', sharedState)
</script><!-- ChildA.vue -->
<template><div><p>收到B的消息: {{ sharedState.messageFromB }}</p><button @click="sendMessage">发送消息给B</button></div>
</template><script setup>
import { inject } from 'vue'const sharedState = inject('sharedState')const sendMessage = () => {sharedState.value.messageFromA = '这是来自A的消息'
}
</script><!-- ChildB.vue -->
<template><div><p>收到A的消息: {{ sharedState.messageFromA }}</p><button @click="sendMessage">发送消息给A</button></div>
</template><script setup>
import { inject } from 'vue'const sharedState = inject('sharedState')const sendMessage = () => {sharedState.value.messageFromB = '这是来自B的消息'
}
</script>

方法三:使用 Vuex/Pinia 状态管理

// stores/messageStore.js
import { defineStore } from 'pinia'export const useMessageStore = defineStore('messages', {state: () => ({messageFromA: '',messageFromB: ''}),actions: {setMessageFromA(message) {this.messageFromA = message},setMessageFromB(message) {this.messageFromB = message}}
})
<!-- ParentComponent.vue -->
<template><ChildA /><ChildB />
</template><script setup>
import ChildA from './ChildA.vue'
import ChildB from './ChildB.vue'
</script><!-- ChildA.vue -->
<template><div><p>收到B的消息: {{ messageStore.messageFromB }}</p><button @click="sendMessage">发送消息给B</button></div>
</template><script setup>
import { useMessageStore } from '@/stores/messageStore'const messageStore = useMessageStore()const sendMessage = () => {messageStore.setMessageFromA('这是来自A的消息')
}
</script><!-- ChildB.vue -->
<template><div><p>收到A的消息: {{ messageStore.messageFromA }}</p><button @click="sendMessage">发送消息给A</button></div>
</template><script setup>
import { useMessageStore } from '@/stores/messageStore'const messageStore = useMessageStore()const sendMessage = () => {messageStore.setMessageFromB('这是来自B的消息')
}
</script>

方法四:使用事件总线(Event Bus)

<!-- ParentComponent.vue -->
<template><ChildA /><ChildB />
</template><script setup>
import ChildA from './ChildA.vue'
import ChildB from './ChildB.vue'
</script><!-- ChildA.vue -->
<template><div><p>收到B的消息: {{ messageFromB }}</p><button @click="sendMessage">发送消息给B</button></div>
</template><script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import eventBus from './eventBus'const messageFromB = ref('')const sendMessage = () => {eventBus.emit('message-to-b', '这是来自A的消息')
}onMounted(() => {eventBus.on('message-to-a', (message) => {messageFromB.value = message})
})onUnmounted(() => {eventBus.off('message-to-a')
})
</script><!-- ChildB.vue -->
<template><div><p>收到A的消息: {{ messageFromA }}</p><button @click="sendMessage">发送消息给A</button></div>
</template><script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import eventBus from './eventBus'const messageFromA = ref('')const sendMessage = () => {eventBus.emit('message-to-a', '这是来自B的消息')
}onMounted(() => {eventBus.on('message-to-b', (message) => {messageFromA.value = message})
})onUnmounted(() => {eventBus.off('message-to-b')
})
</script>

http://www.dtcms.com/a/273610.html

相关文章:

  • Vue 3 入门——自学习版本
  • DOM编程实例(不重要,可忽略)
  • 分享|2025年机器学习工程师职业技术证书报考指南
  • 论容器化 | 分析Go和Rust做医疗的后端服务
  • 在vue中使用Three.js渲染FBX模型
  • arcgis api for js 设置地图服务请求带有请求头信息
  • 录音实时上传
  • uniapp
  • Claude Code是什么?国内如何使用到Claude Code?附国内最新使用教程
  • 基于定制开发开源AI智能名片与S2B2C商城小程序的旅游日志创新应用研究
  • uniapp小程序tabbar跳转拦截与弹窗控制
  • Elasticsearch混合搜索深度解析(上):问题发现与源码探索
  • Excel 转 JSON by WTSolutions API 文档
  • 较为深入的了解c++中的string类(2)
  • MyBatis 从入门到实战:代理 Dao 模式下的 CRUD 全解析
  • Netplan 配置网桥(Bridge)的模板笔记250711
  • excel如何只保留前几行
  • 提示工程:解锁大模型潜力的核心密码
  • 基于redis的分布式session共享管理之销毁事件不生效问题
  • 这个方法的目的是检查一个给定的项目ID(projectId)是否在当前数据库中被使用(搜索全库)
  • SortByCustomOrder 根据指定的顺序对任意类型的列表进行排序
  • Python七彩花朵
  • 【实时Linux实战系列】实时系统测试与合规认证指南
  • 二刷 黑马点评 商户查询缓存
  • <script>标签对HTML文件解析过程的影响以及async和defer属性的应用
  • 在 React Three Fiber 中实现 3D 模型点击扩散波效果
  • 车企战略投资项目管理的实践与思考︱中国第一汽车集团进出口有限公司战略部投资管理专家庞博
  • 台球 PCOL:极致物理还原的网页斯诺克引擎(附源码深度解析)
  • 软件设计师中级逻辑公式题
  • Ubuntu 24.04上安装 Intelligent Pinyin 中文输入法