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.pswp.cn/bicheng/88712.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/88712.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/88712.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

三子棋游戲設計與實現(C 語言版)

一、需求分析目標&#xff1a;實現一個簡單的人機對戰三子棋&#xff0c;支持以下功能&#xff1a;初始化空棋盤&#xff0c;清晰展示落子狀態。玩家通過坐標落子&#xff08;X 代表玩家&#xff09;&#xff0c;電腦隨機落子&#xff08;O 代表電腦&#xff09;。實時判斷勝負…

GD32 CAN1和TIMER0同時開啟問題

背景&#xff1a;今天在一個項目調試的時候發現了一些問題&#xff0c;由此貼記錄一下問題解決的過程。使用的芯片是GD32F305VE。使用到了CAN1和TIMER0。在使用這連個外設的時候發送了一些問題。單獨使用CAN1。功能正常。單獨使用TIMER0。配置為輸出模式。功能正常。但是當兩個…

劍指offer56_數組中唯一只出現一次的數字

數組中唯一只出現一次的數字在一個數組中除了一個數字只出現一次之外&#xff0c;其他數字都出現了三次。 請找出那個只出現一次的數字。 你可以假設滿足條件的數字一定存在。 思考題&#xff1a; 如果要求只使用 O(n) 的時間和額外 O(1) 的空間&#xff0c;該怎么做呢&#xf…

從語音識別到智能助手:Voice Agent 的技術進化與交互變革丨Voice Agent 學習筆記

From Research AI&#xff1a; 最近看到 Andrew Ng 的一句話讓我印象深刻&#xff1a;“While some things in AI are overhyped, voice applications seem underhyped right now.”&#xff08;盡管 AI 中有些領域被過度炒作&#xff0c;語音應用卻似乎被低估了&#xff09;。…

什么是Jaccard 相似度(Jaccard Similarity)

文章目錄? 定義&#xff1a;&#x1f4cc; 取值范圍&#xff1a;&#x1f50d; 舉例說明&#xff1a;&#x1f9e0; 應用場景&#xff1a;?? 局限性&#xff1a;&#x1f4a1; 擴展概念&#xff1a;Jaccard 相似度&#xff08;Jaccard Similarity&#xff09; 是一種用于衡量…

ragflow_多模態文檔解析與正文提取策略

多模態文檔解析與正文提取策略 RAGflow的文檔解析系統位于deepdoc/parser/目錄下,實現了對多種文檔格式的統一解析處理。該系統采用模塊化設計,針對不同文檔格式提供專門的解析器,并通過視覺識別技術增強解析能力。本文將深入探討RAGflow的文檔解析系統的設計原理、實現細節…

數據結構棧的實現(C語言)

棧的基本概念棧是一種特殊的線性存儲結構&#xff0c;是一種操作受到限制的線性表&#xff0c;特殊體現在兩個地方&#xff1a;1、元素進棧出棧的操作只能從同一端完成&#xff0c;另一端是封閉的&#xff0c;通常將數據進棧叫做入棧&#xff0c;壓棧等&#xff0c;出棧叫做彈棧…

【springboot】IDEA手動創建SpringBoot簡單工程(無插件)

大致步驟 創建Maven工程 引入依賴 提供啟動類 詳細教程 創建Maven工程 修改pom.xml文件 添加父節點 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.3</…

獨立開發第二周:構建、執行、規劃

一 第二周的獨立開發旅程落下帷幕。相較于第一周的適應&#xff0c;本周的核心詞是“聚焦”與“執行”。 目標非常明確&#xff1a;在產品開發上取得進展&#xff1b;在個人工作節奏上&#xff0c;將上周初步形成的框架進行實踐與固化。 同時&#xff0c;為至關重要的自媒體運營…

在YOLO-World中集成DeformConv、CBAM和Cross-Modal Attention模塊的技術報告

在YOLO-World中集成DeformConv、CBAM和Cross-Modal Attention模塊的技術報告 1. 引言 1.1 項目背景 目標檢測是計算機視覺領域的核心任務之一,而YOLO(You Only Look Once)系列算法因其出色的速度和精度平衡而廣受歡迎。YOLO-World是YOLO系列的最新發展,專注于開放詞匯目標…

從UI設計到數字孿生實戰應用:構建智慧金融的風險評估與預警平臺

hello寶子們...我們是艾斯視覺擅長ui設計、前端開發、數字孿生、大數據、三維建模、三維動畫10年經驗!希望我的分享能幫助到您!如需幫助可以評論關注私信我們一起探討!致敬感謝感恩!一、引言&#xff1a;傳統金融風控的 “滯后困境” 與數字孿生的破局之道金融風險的隱蔽性、突…

【Linux】權限相關指令

前言&#xff1a; 上兩篇文章我們講到了&#xff0c;關于Linux中的基礎指令。 【Linux】初見&#xff0c;基礎指令-CSDN博客【Linux】初見&#xff0c;基礎指令&#xff08;續&#xff09;-CSDN博客 本文我們來講Linux中關于權限中的一些指令 shell命令 Linux嚴格來說是一個操…

前端學習3--position定位(relative+absolute+sticky)

繼上一篇&#xff0c;做下拉菜單的時候&#xff0c;涉及到了position&#xff0c;這篇就來學習下~先看下position在下拉菜單中的應用&#xff1a;一、關鍵代碼回顧&#xff1a;/* 下拉菜單容器 */ .dropdown {position: relative; /* ? 關鍵父級 */ }/* 下拉內容&#xff08;默…

APP Inventor使用指南

APP Inventor使用指南一、組件介紹二、邏輯設計設計方法&#xff1a;設計實例&#xff08;參考嘉立創教程&#xff09;點擊跳轉 &#xff1a; app inventor&#xff08;點不開的話需要&#x1fa84;&#x1fa84;&#x1fa84;&#x1fa84;&#x1fa84;&#xff09; 一、組…

SpringAI實現保存聊天記錄到redis中

redis相關準備添加依賴我利用redission來實現<dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.37.0</version> </dependency>添加配置文件spring:redis:database: 5host: 127.0.0.1…

Unity中使用EzySlice實現模型切割與UV控制完全指南

引言 在Unity中實現3D模型的動態切割是一個常見的需求&#xff0c;無論是用于游戲特效、建筑可視化還是醫療模擬。本文將全面介紹如何使用EzySlice插件實現高效的模型切割&#xff0c;并深入探討如何通過Shader Graph精確控制切割面的UV映射。 第一部分&#xff1a;EzySlice基…

【c++學習記錄】狀態模式,實現一個登陸功能

狀態模式建議為對象的所有可能狀態新建一個類&#xff0c; 然后將所有狀態的對應行為抽取到這些類中。 原始對象被稱為上下文 &#xff08;context&#xff09;&#xff0c; 它并不會自行實現所有行為&#xff0c; 而是會保存一個指向表示當前狀態的狀態對象的引用&#xff0c;…

Docker 搭建 Harbor 私有倉庫

1 部署 Harbor 注意&#xff1a;docker、docker-compose、Harbor的版本是否適配&#xff0c;這里使用的版本如下表&#xff1a; Docker版本Docker Compose版本Harbor版本v19.09.8v1.29.2v2.8.2 1.1 安裝 docker-compose # 下載 docker-compose 1.29.2 版本 curl -L "h…

C++類模板繼承部分知識及測試代碼

目錄 0.前言 1.類模板基本使用 2.類模板繼承 2.1類模板繼承過程中的模板參數 情況1&#xff1a;父類非模板&#xff0c;子類為模板 情況2&#xff1a;父類模板&#xff0c;子類為非模板 情況3&#xff1a;父類模板&#xff0c;子類為模板 3.STL中的模板類分析 3.1STL中…

Laravel + Python 圖片水印系統:實現與調試指南

前言 本系統通過 Laravel 作為前端框架接收用戶上傳的圖片&#xff0c;調用 Python 腳本處理水印添加&#xff0c;最終返回處理后的圖片。這種架構充分利用了 Laravel 的便捷性和 Python 圖像處理庫的強大功能。 一、Python 水印處理腳本 from PIL import Image, ImageEnhance …