Vue組件之間四種通信方式詳解
前言
vue 框架提供了前端開(kāi)發(fā)組件的思想,可以通過(guò)組件來(lái)組合成一個(gè)完整的頁(yè)面,都是隨著組件數(shù)量原來(lái)越多,組件之間難免需要相互通信,那么如何實(shí)現(xiàn)組件之間的通信呢?下面介紹 vue 組件通信的幾種方法
父子組件通信?
父組件傳遞 props 給子組件(數(shù)據(jù)以及改變數(shù)據(jù)的方法),子組件通過(guò) $emit 來(lái)更新父組件的狀態(tài)
父組件定義,聲明狀態(tài) { name: 'baidu.com' } 以及可以改變狀態(tài)的方法update(),通過(guò) name 傳遞和 @update 把 name 屬性和 update 方法傳遞給子組件

<template>
<div>
<Child : @update="update" />
</div>
</template>
<script>
import Child from "./components/Child";
export default {
name: "App",
components: {
Child,
},
data() {
return {
name: "baidu.com",
};
},
methods: {
update() {
this.name = "www.baidu.com";
},
},
};
</script>vue
子組件的定義,定義 props 接收 name 屬性,通過(guò) $emit 傳遞 update 參數(shù)通知父組件更新?tīng)顟B(tài)
<template>
<div>
{{ name }}
<button @click="$emit('update')">通知父組件數(shù)據(jù)</button>
</div>
</template>
<script>
export default {
name: "Child",
props: {
name: String,
},
};
</script>父組件與子孫組件的通信?
父組件通過(guò) provide 創(chuàng)建對(duì)象,子組件通過(guò) inject 來(lái)使用父組件的數(shù)據(jù),不受組件層級(jí)的限制
父組件通過(guò) provide 定義需要傳遞是數(shù)據(jù)
<template>
<div>
<Child />
</div>
</template>
<script>
import Child from "./components/Child";
export default {
name: "App",
components: {
Child,
},
provide: {
name: "www.baidu.com",
},
};
</script>子組件通過(guò) inject 屬性控制哪些屬性需要訪問(wèn)
<template>
<div>{{ name }}</div>
</template>
<script>
export default {
name: "Child",
inject: ["name"],
};
</script>父組件獲取子組件數(shù)據(jù)?
通過(guò)ref 來(lái)獲取子組件的實(shí)例,可以獲取子組件的狀態(tài)或者調(diào)用子組件的方法,例如下面
<template>
<div>
<Child ref="child" />
</div>
</template>
<script>
import Child from "./components/Child";
export default {
name: "App",
components: {
Child,
},
mounted() {
console.log(this.$refs.child.title);
},
};
</script>可以通過(guò) this.$refs.child 獲取到 Child 組件的實(shí)例,訪問(wèn) Child 組件的 data
無(wú)需考慮組件關(guān)系的通信?
通過(guò) vue 提供的發(fā)布訂閱模式,也叫做 EventBus(事件總線)
定義一個(gè) eventBus 實(shí)例
import Vue from "vue"; export default new Vue();
父組件在 mounted 生命周期里面通過(guò) $on 監(jiān)聽(tīng) update 事件
<template>
<div>
<Child : />
</div>
</template>
<script>
import Child from "./components/Child";
import eBus from "../eventBus";
export default {
name: "App",
data() {
return {
name: "baidu.com",
};
},
components: {
Child,
},
mounted() {
eBus.$on("update", (val) => {
this.name = val;
});
},
};
</script>子組件通過(guò) vue 實(shí)例的 $emit 觸發(fā) update 事件
<template>
<div>
{{ name }}
<button @click="clickHandle">通知父組件更新</button>
</div>
</template>
<script>
import eBus from "../../eventBus";
export default {
name: "Child",
props: {
name: String,
},
data() {
return {
title: "child component",
};
},
methods: {
clickHandle() {
eBus.$emit("update", "Hello World");
},
},
};
</script>使用全局狀態(tài)管理庫(kù) vuex
具體就不在這里展開(kāi)講,有興趣的可以查閱 vue 官方文檔
到此這篇關(guān)于Vue組件之間四種通信方式詳解的文章就介紹到這了,更多相關(guān)Vue組件通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3動(dòng)態(tài)路由刷新出現(xiàn)空白頁(yè)的原因與最優(yōu)解
頁(yè)面刷新白屏其實(shí)是因?yàn)関uex引起的,由于刷新頁(yè)面vuex數(shù)據(jù)會(huì)丟失,這篇文章主要給大家介紹了關(guān)于vue3動(dòng)態(tài)路由刷新出現(xiàn)空白頁(yè)的原因與最優(yōu)解的相關(guān)資料,需要的朋友可以參考下2023-11-11
Vue2.0實(shí)現(xiàn)簡(jiǎn)單分頁(yè)及跳轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Vue2.0實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)分頁(yè),及頁(yè)數(shù)的跳轉(zhuǎn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Vue運(yùn)用transition實(shí)現(xiàn)過(guò)渡動(dòng)畫
vue的過(guò)渡動(dòng)畫,主要是transition標(biāo)簽的使用,配合css動(dòng)畫實(shí)現(xiàn)的。接下來(lái)通過(guò)本文給大家分享Vue運(yùn)用transition實(shí)現(xiàn)過(guò)渡動(dòng)畫效果,感興趣的朋友一起看看吧2019-05-05
Vue2.0使用過(guò)程常見(jiàn)的一些問(wèn)題總結(jié)學(xué)習(xí)
本篇文章主要介紹了Vue2.0使用過(guò)程常見(jiàn)的一些問(wèn)題總結(jié)學(xué)習(xí),詳細(xì)的介紹了使用中會(huì)遇到的各種錯(cuò)誤,有興趣的可以了解一下。2017-04-04
vue之組件內(nèi)監(jiān)控$store中定義變量的變化詳解
今天小編就為大家分享一篇vue之組件內(nèi)監(jiān)控$store中定義變量的變化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue實(shí)現(xiàn)點(diǎn)擊復(fù)制到粘貼板
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)點(diǎn)擊復(fù)制到粘貼板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

