vue3中實(shí)現(xiàn)組件通信的方法總結(jié)
在 Vue 3 中,有多種方法可以實(shí)現(xiàn)組件之間的通信。以下是一些常見的方法:
1、Props 和 Events:父組件通過 props 向子組件傳遞數(shù)據(jù),子組件通過觸發(fā)事件向父組件發(fā)送消息。這是 Vue 中最基本的組件通信方式
props接收的數(shù)據(jù)是只讀的
<!-- 父組件 -->
<template>
<ChildComponent :message="parentMessage" @childEvent="handleChildEvent" />
</template>
<script setup lang="ts">
import ChildComponent from './ChildComponent.vue';
import {ref} from 'vue'
let parentMessage = ref('Hello from parent'),
const handleChildEvent = (payload) => {
console.log('Received event from child:', payload);
}
</script>
<!-- 子組件 -->
<template>
<div>
<p>{{ props.message }}</p>
<p>{{ message }}</p>
<button @click="emitEvent">Send Event to Parent</button>
</div>
</template>
<script setup lang="ts">
import {defineEmits} from 'vue'
const emit = defineEmits();
// 使用defineProperty來接收數(shù)據(jù),defineProperty不需引入直接使用即可defineProps({})或defineProps([]),
// props中的是代理對(duì)象,在模板中使用可以省略props,在js中不可省略
const props = defineProps({ message: String});
const emitEvent = () =>{
$emit('childEvent', 'Hello from child');
}
</script>2、Provide 和 Inject:這是一種在組件樹中跨級(jí)傳遞數(shù)據(jù)的方法。祖先組件通過 provide 選項(xiàng)提供數(shù)據(jù),后代組件通過 inject 選項(xiàng)接收數(shù)據(jù)。
// 祖先組件
export default {
provide() {
return {
sharedData: 'Shared data from ancestor',
};
},
};
// 后代組件
export default {
inject: ['sharedData'],
};3、Vuex:Vuex 是一個(gè)專門為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它可以幫助你管理應(yīng)用程序中的共享狀態(tài),實(shí)現(xiàn)組件之間的通信。
首先,你需要安裝 Vuex 并在你的應(yīng)用程序中配置它:
npm install vuex@next --save
然后,創(chuàng)建一個(gè) Vuex store:
// store.js
import { createStore } from 'vuex';
export default createStore({
state: {
count: 0,
},
mutations: {
increment(state) {
state.count++;
},
},
});在你的應(yīng)用程序中使用 store:
// main.js
import { createApp } from 'vue';
import App from './App.vue';
import store from './store';
createApp(App).use(store).mount('#app');現(xiàn)在,你可以在組件中使用 this.$store 訪問 store,并通過提交 mutation 或 dispatching action 來更新狀態(tài)。
<!-- 組件 -->
<template>
<div>
<p>{{ $store.state.count }}</p>
<button @click="$store.commit('increment')">Increment</button>
</div>
</template>這些方法可以根據(jù)你的需求和應(yīng)用程序的復(fù)雜性來選擇。對(duì)于簡(jiǎn)單的組件通信,Props 和 Events 通常就足夠了。對(duì)于更復(fù)雜的應(yīng)用程序,你可能需要考慮使用 Provide/Inject 或 Vuex。
4、類似vue2中的eventBus插件mitt 實(shí)現(xiàn)全局通信
到此這篇關(guān)于vue3中實(shí)現(xiàn)組件通信的方法總結(jié)的文章就介紹到這了,更多相關(guān)vue3組件通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js響應(yīng)式原理解析與實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了vue.js響應(yīng)式原理解析與實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
解決vue-router 二級(jí)導(dǎo)航默認(rèn)選中某一選項(xiàng)的問題
今天小編就為大家分享一篇解決vue-router 二級(jí)導(dǎo)航默認(rèn)選中某一選項(xiàng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容
這篇文章主要介紹了vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue?Element?UI擴(kuò)展內(nèi)容過長(zhǎng)使用tooltip顯示
這篇文章主要為大家介紹了vue?Element?UI擴(kuò)展內(nèi)容過長(zhǎng)使用tooltip展示鼠標(biāo)hover時(shí)的提示信息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
vue中的 $slot 獲取插槽的節(jié)點(diǎn)實(shí)例
今天小編就為大家分享一篇vue中的 $slot 獲取插槽的節(jié)點(diǎn)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue中動(dòng)態(tài)修改animation效果無效問題詳情
這篇文章主要介紹了vue中動(dòng)態(tài)修改animation效果無效問題詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
vue3中reactive數(shù)據(jù)被重新賦值后無法雙向綁定的解決
這篇文章主要介紹了vue3中reactive數(shù)據(jù)被重新賦值后無法雙向綁定的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

