Vue中$bus的用法及$on、$off的使用說明
$bus的用法及$on、$off的使用
Vue中 $bus一般與 $on、 $off連用,一般用在任意組件間的通訊,所以今天就一起說,這里給大家舉一個(gè)案例
首先在main.js中配置全局總線
//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//關(guān)閉Vue的生產(chǎn)提示
Vue.config.productionTip = false
//創(chuàng)建vm
new Vue({
?? ?el:'#app',
?? ?render: h => h(App),
?? ?beforeCreate(){ ? ? ? ? ? ? ? ? //在初始化階段前
?? ??? ?Vue.prototype.$bus = this ? //配置全局總線,bus有總線的意思
?? ?}
})在Student組件中提供數(shù)據(jù)給另一個(gè)組件(School組件)
<template>
?? ?<div class="student">
?? ??? ?<h2>學(xué)生姓名:{{name}}</h2>
?? ??? ?<h2>學(xué)生性別:{{sex}}</h2>
?? ??? ?<button @click="sendStudentName">把學(xué)生名給School組件</button>
?? ?</div>
</template><script>
?? ?export default {
?? ??? ?name:'Student',
?? ??? ?data() {
?? ??? ??? ?return {
?? ??? ??? ??? ?name:'張三',
?? ??? ??? ??? ?sex:'男',
?? ??? ??? ?}
?? ??? ?},
?? ??? ?methods: {
?? ??? ??? ?sendStudentName(){ ? ? ? ?//提供發(fā)送數(shù)據(jù)
?? ??? ??? ??? ?this.$bus.$emit('hello',this.name)
?? ??? ??? ?}
?? ??? ?},
?? ?}
</script>在School組件中使用事件總線,接收數(shù)據(jù)。School組件想接收數(shù)據(jù),則在School組件中給 $bus綁定事件,事件的回調(diào)則留在School組件自身。
<template>
?? ?<div class="school">
?? ??? ?<h2>學(xué)校名稱:{{name}}</h2>
?? ??? ?<h2>學(xué)校地址:{{address}}</h2>
?? ?</div>
</template><script>
?? ?export default {
?? ??? ?name:'School',
?? ??? ?data() {
?? ??? ??? ?return {
?? ??? ??? ??? ?name:'工程學(xué)院',
?? ??? ??? ??? ?address:'重慶巴南',
?? ??? ??? ?}
?? ??? ?},
?? ??? ?mounted() {
?? ??? ??? ?this.$bus.$on('hello',(data)=>{ ? ? ?//綁定當(dāng)前事件(這里以hello為例)
?? ??? ??? ??? ?console.log('我是School組件,收到了數(shù)據(jù)',data)
?? ??? ??? ?})
?? ??? ?},
?? ??? ?beforeDestroy(){ ? ? ? ?//收尾操作,銷毀
?? ??? ??? ?this.$bus.$off('hello') ?//$off解綁當(dāng)前組件所用到的事件
?? ??? ?}
?? ?}
</script>到這里已經(jīng)結(jié)束了,再給一個(gè)App組件,大家可以試一試這個(gè)案例
<template> ?? ?<div class="app"> ?? ??? ?<School/> ?? ??? ?<Student/> ?? ?</div> </template>
<script>
?? ?import Student from './components/Student'
?? ?import School from './components/School'
?? ?export default {
?? ??? ?name:'App',
?? ??? ?components:{School,Student},
?? ?}
</script>vue實(shí)例事件總結(jié)($on,$once,$off,$emit)
1.什么是實(shí)例事件?
實(shí)例事件就是在構(gòu)造器外部調(diào)用構(gòu)造器內(nèi)部的數(shù)據(jù)。
2.使用場景
比如構(gòu)造器已經(jīng)有一個(gè)加的方法,現(xiàn)在領(lǐng)導(dǎo)說還要做一個(gè)減的方法,而我又不想動(dòng)構(gòu)造器里面的信息,那就可以使用實(shí)例事件了.操作如下:

3.與$on對(duì)應(yīng)的還有一個(gè)$once
上述代碼只改一個(gè)單詞$once
app.$once('reduce',function(){
console.log("減的方法觸發(fā)了");
this.num--
})
結(jié)果:減的方法只執(zhí)行一次.
4.$off關(guān)閉實(shí)例事件
關(guān)閉reduce后,減就失效了.

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3父子組件通信、兄弟組件實(shí)時(shí)通信方式
這篇文章主要介紹了vue3父子組件通信、兄弟組件實(shí)時(shí)通信方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
詳解基于iview-ui的導(dǎo)航欄路徑(面包屑)配置
這篇文章主要介紹了詳解基于iview-ui的導(dǎo)航欄路徑(面包屑)配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
Vue中的異步組件函數(shù)實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue中的異步組件函數(shù)實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
vue el-table 動(dòng)態(tài)添加行與刪除行的實(shí)現(xiàn)
這篇文章主要介紹了vue el-table 動(dòng)態(tài)添加行與刪除行的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

