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)聽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之間通過全局?jǐn)?shù)據(jù)總線進(jìn)行數(shù)據(jù)傳遞。
在main.js中,定義了全局事件總線:$bus。
$bus定義在Vue.prototype,因此$bus對(duì)所有組件可見,即所有組件可通過this.$bus訪問。
$bus被賦值為this,即vm實(shí)例,因此$bus擁有vm實(shí)例上的所有屬性和方法,如$emit、$on、$off等。
new Vue({
beforeCreate(){
Vue.prototype.$bus = this;
}
})
使用全局事件總線
$bus.$on,監(jiān)聽事件。Employee組件中定義了監(jiān)聽事件,監(jiān)聽demo事件;
$bus.$emit,觸發(fā)事件。Company組件中定義了觸發(fā)事件,點(diǎn)擊按鈕執(zhí)行sendMessage回調(diào),該回調(diào)將觸發(fā)demo事件。


總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
結(jié)合mint-ui移動(dòng)端下拉加載實(shí)踐方法總結(jié)
下面小編就為大家?guī)硪黄Y(jié)合mint-ui移動(dòng)端下拉加載實(shí)踐方法總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
vue2.0 可折疊列表 v-for循環(huán)展示的實(shí)例
今天小編大家分享一篇vue2.0 可折疊列表 v-for循環(huán)展示的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
在Vue項(xiàng)目中引入JQuery-ui插件的講解
今天小編就為大家分享一篇關(guān)于在Vue項(xiàng)目中引入JQuery-ui插件的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
vue最強(qiáng)table vxe-table 虛擬滾動(dòng)列表 前端導(dǎo)出問題分析
最近遇到個(gè)問題,后臺(tái)一次性返回2萬條列表數(shù)據(jù)并且需求要求所有數(shù)據(jù)必須全部展示,不能做假分頁,怎么操作呢,下面通過本文介紹下vue最強(qiáng)table vxe-table 虛擬滾動(dòng)列表 前端導(dǎo)出問題,感興趣的朋友一起看看吧2023-10-10
利用vuex-persistedstate將vuex本地存儲(chǔ)實(shí)現(xiàn)
這篇文章主要介紹了利用vuex-persistedstate將vuex本地存儲(chǔ)的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
利用Dectorator分模塊存儲(chǔ)Vuex狀態(tài)的實(shí)現(xiàn)
這篇文章主要介紹了利用Dectorator分模塊存儲(chǔ)Vuex狀態(tài)的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
vscode+vue cli3.0創(chuàng)建項(xiàng)目配置Prettier+eslint方式
這篇文章主要介紹了vscode+vue cli3.0創(chuàng)建項(xiàng)目配置Prettier+eslint方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
v-show和v-if的區(qū)別?及應(yīng)用場(chǎng)景
這篇文章主要介紹了v-show和v-if的區(qū)別及應(yīng)用場(chǎng)景,vue中v-show與?v-if的作用效果是相同的,都能控制元素在頁面是否顯示,但是也有一定的區(qū)別,下面文章梳理總結(jié)v-show和v-if的區(qū)別,需要的小伙伴可以參考一下2022-06-06

