Vue全局事件總線你了解嗎
全局事件總線,是組件間的一種通信方式,適用于任何組件間通信。
看下面具體的例子。
父組件:App
<template>
<div class="app">
<Company/>
<Employee/>
</div>
</template>
<script>
import Company from "./components/Company.vue";
import Employee from "./components/Employee.vue";
export default {
components:{
Company,
Employee
}
}
</script>
<style>
.app{
background: gray;
padding: 5px;
}
.btn{
margin-left:10px;
line-height: 30px;
background: ivory;
border-radius: 5px;
}
</style>
子組件:Company和Employee
<template>
<div class="company">
<h2>公司名稱:{{name}}</h2>
<h2>公司地址:{{address}}</h2>
<button @click="sendMessage">點(diǎn)我發(fā)送</button>
</div>
</template>
<script>
export default {
name:"Company",
data(){
return {
name:"五哈技術(shù)有限公司",
address:"上海寶山"
}
},
methods:{
sendMessage(){
console.log("Company組件發(fā)送數(shù)據(jù):",this.name);
this.$bus.$emit("demo",this.name);
}
}
}
</script>
<style scoped>
.company{
background: orange;
background-clip: content-box;
padding: 10px;
}
</style>
<template>
<div class="employee">
<h2>員工姓名:{{name}}</h2>
<h2>員工年齡:{{age}}</h2>
</div>
</template>
<script>
export default {
name:"Employee",
data(){
return {
name:"張三",
age:25
}
},
mounted(){
this.$bus.$on("demo",(data) => {
console.log("Employee組件監(jiān)聽(tīng)demo,接收數(shù)據(jù):",data);
})
},
beforeDestroy() {
this.$bus.$off("demo");
}
}
</script>
<style scoped>
.employee{
background: skyblue;
background-clip: content-box;
padding: 10px;
}
</style>
入口文件:main.js
import Vue from 'vue';
import App from './App.vue';
Vue.config.productionTip = false;
new Vue({
el:"#app",
render: h => h(App),
beforeCreate(){
Vue.prototype.$bus = this;
}
})
父組件App,子組件Company和Employee
子組件Company和Employee之間通過(guò)全局?jǐn)?shù)據(jù)總線進(jìn)行數(shù)據(jù)傳遞。
在main.js中,定義了全局事件總線:$bus。
$bus定義在Vue.prototype,因此$bus對(duì)所有組件可見(jiàn),即所有組件可通過(guò)this.$bus訪問(wèn)。
$bus被賦值為this,即vm實(shí)例,因此$bus擁有vm實(shí)例上的所有屬性和方法,如$emit、$on、$off等。
new Vue({
beforeCreate(){
Vue.prototype.$bus = this;
}
})
使用全局事件總線
$bus.$on,監(jiān)聽(tīng)事件。Employee組件中定義了監(jiān)聽(tīng)事件,監(jiān)聽(tīng)demo事件;
$bus.$emit,觸發(fā)事件。Company組件中定義了觸發(fā)事件,點(diǎn)擊按鈕執(zhí)行sendMessage回調(diào),該回調(diào)將觸發(fā)demo事件。


總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Vue項(xiàng)目中vue.config.js常用配置項(xiàng)詳解
在 Vue CLI 創(chuàng)建的項(xiàng)目中,vue.config.js 是核心配置文件,用于定制化構(gòu)建、開(kāi)發(fā)和部署流程,本文詳細(xì)解析了該文件的常用配置項(xiàng),并結(jié)合代碼示例和表格說(shuō)明,幫助開(kāi)發(fā)者高效管理項(xiàng)目配置,提升開(kāi)發(fā)體驗(yàn),需要的朋友可以參考下2025-04-04
Vue單頁(yè)面應(yīng)用做預(yù)渲染的方法實(shí)例
vue是一個(gè)單頁(yè)面的應(yīng)用,這導(dǎo)致一些爬蟲(chóng)和百度無(wú)法搜索到,如果你想針對(duì)你應(yīng)用的其中某些頁(yè)面進(jìn)行SEO優(yōu)化,讓他們可以被爬蟲(chóng)和百度搜索到,你可以進(jìn)行預(yù)渲染操作,下面這篇文章主要給大家介紹了關(guān)于Vue單頁(yè)面應(yīng)用做預(yù)渲染的相關(guān)資料,需要的朋友可以參考下2021-10-10
vue新建環(huán)境變量以及網(wǎng)絡(luò)請(qǐng)求工具axios的二次封裝詳解
這篇文章主要為大家介紹了vue新建環(huán)境變量以及網(wǎng)絡(luò)請(qǐng)求工具axios的二次封裝詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
vue中的Object.freeze()?優(yōu)化數(shù)據(jù)方式
這篇文章主要介紹了vue中的Object.freeze()優(yōu)化數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue.sync修飾符與$emit(update:xxx)詳解
這篇文章主要介紹了Vue.sync修飾符與$emit(update:xxx),實(shí)現(xiàn)思路非常簡(jiǎn)單,文章介紹了.sync修飾符的作用和使用.sync修飾符的寫(xiě)法,實(shí)現(xiàn)代碼簡(jiǎn)單易懂對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
Vue3哈希模式實(shí)現(xiàn)錨點(diǎn)導(dǎo)航方式
這篇文章主要介紹了Vue3哈希模式實(shí)現(xiàn)錨點(diǎn)導(dǎo)航方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
案例實(shí)操vue事件修飾符帶你快速了解與應(yīng)用
這篇文章主要介紹了vue常見(jiàn)的事件修飾符,在平時(shí)無(wú)論是面試還是學(xué)習(xí)工作中都會(huì)經(jīng)常遇到的,本文就帶你快速上手,需要的朋友可以參考下2023-03-03

