Vue組件之間四種通信方式詳解
前言
vue 框架提供了前端開發(fā)組件的思想,可以通過組件來組合成一個完整的頁面,都是隨著組件數(shù)量原來越多,組件之間難免需要相互通信,那么如何實現(xiàn)組件之間的通信呢?下面介紹 vue 組件通信的幾種方法
父子組件通信?
父組件傳遞 props 給子組件(數(shù)據(jù)以及改變數(shù)據(jù)的方法),子組件通過 $emit 來更新父組件的狀態(tài)
父組件定義,聲明狀態(tài) { name: 'baidu.com' } 以及可以改變狀態(tài)的方法update(),通過 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 屬性,通過 $emit 傳遞 update 參數(shù)通知父組件更新狀態(tài)
<template>
<div>
{{ name }}
<button @click="$emit('update')">通知父組件數(shù)據(jù)</button>
</div>
</template>
<script>
export default {
name: "Child",
props: {
name: String,
},
};
</script>父組件與子孫組件的通信?
父組件通過 provide 創(chuàng)建對象,子組件通過 inject 來使用父組件的數(shù)據(jù),不受組件層級的限制
父組件通過 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>子組件通過 inject 屬性控制哪些屬性需要訪問
<template>
<div>{{ name }}</div>
</template>
<script>
export default {
name: "Child",
inject: ["name"],
};
</script>父組件獲取子組件數(shù)據(jù)?
通過ref 來獲取子組件的實例,可以獲取子組件的狀態(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>可以通過 this.$refs.child 獲取到 Child 組件的實例,訪問 Child 組件的 data
無需考慮組件關(guān)系的通信?
通過 vue 提供的發(fā)布訂閱模式,也叫做 EventBus(事件總線)
定義一個 eventBus 實例
import Vue from "vue"; export default new Vue();
父組件在 mounted 生命周期里面通過 $on 監(jiān)聽 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>子組件通過 vue 實例的 $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)管理庫 vuex
具體就不在這里展開講,有興趣的可以查閱 vue 官方文檔
到此這篇關(guān)于Vue組件之間四種通信方式詳解的文章就介紹到這了,更多相關(guān)Vue組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3動態(tài)路由刷新出現(xiàn)空白頁的原因與最優(yōu)解
頁面刷新白屏其實是因為vuex引起的,由于刷新頁面vuex數(shù)據(jù)會丟失,這篇文章主要給大家介紹了關(guān)于vue3動態(tài)路由刷新出現(xiàn)空白頁的原因與最優(yōu)解的相關(guān)資料,需要的朋友可以參考下2023-11-11
Vue2.0實現(xiàn)簡單分頁及跳轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Vue2.0實現(xiàn)簡單數(shù)據(jù)分頁,及頁數(shù)的跳轉(zhuǎn)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
Vue2.0使用過程常見的一些問題總結(jié)學(xué)習(xí)
本篇文章主要介紹了Vue2.0使用過程常見的一些問題總結(jié)學(xué)習(xí),詳細(xì)的介紹了使用中會遇到的各種錯誤,有興趣的可以了解一下。2017-04-04
vue之組件內(nèi)監(jiān)控$store中定義變量的變化詳解
今天小編就為大家分享一篇vue之組件內(nèi)監(jiān)控$store中定義變量的變化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

