vue中EventBus的使用教程詳解
示例背景
在Vue中,使用EventBus可以實(shí)現(xiàn)組件間的通信,如何使用EventBus? 都需要做哪些配置呢?他的注意事項(xiàng)是什么呢?請(qǐng)參考以下示例及使用步驟。
示例效果圖
示例源代碼
父組件:
/* * @Author: 大劍師蘭特(xiaozhuanlan),還是大劍師蘭特(CSDN) * @此源代碼版權(quán)歸大劍師蘭特所有,可供學(xué)習(xí)或商業(yè)項(xiàng)目中借鑒,未經(jīng)授權(quán),不得重復(fù)地發(fā)表到博客、論壇,問答,git等公共空間或網(wǎng)站中。 * @Email: 2909222303@qq.com * @weixin: gis-dajianshi * @First published in CSDN * @First published time: 2024-02-03 */ <template> <div class="djs-box"> <div class="topBox"> <h3>vue使用EventBus的圖文示例 </h3> <div>大劍師蘭特, 還是大劍師蘭特,gis-dajianshi</div> </div> <div class="dajianshi"> <div class="item"> <ComA></ComA> </div> <div class="item"> <ComB></ComB> </div> </div> </div> </template> <script> import ComA from '../components/eventbus/demo-A.vue'; import ComB from '../components/eventbus/demo-B.vue' export default { data() { return {} }, components:{ ComA, ComB }, } </script> <style scoped> .djs-box { width: 1000px; height: 650px; margin: 50px auto; border: 1px solid peru; } .topBox { margin: 0 auto 0px; padding: 10px 0 20px; background: peru; color: #fff; } .dajianshi { width: 98%; height: 480px; margin: 5px auto 0; display: flex; justify-content: space-between; } .item{ width: 48%; height: 480px; margin-top: 20px; text-align: center; border: 1px solid #583; } </style>
子組件A:
<template> <div> <h2> 組件A:</h2> <el-button @click="sendMessage">發(fā)送消息給B</el-button> <h4> 從B獲取的消息:</h4> <div style="color: blue;">{{message}}</div> </div> </template> <script> import { EventBus } from '@/eventbus/index.js'; export default { data() { return { message: '' }; }, mounted() { EventBus.$on('messageB', (msg) => { this.message = msg; }); }, methods: { sendMessage() { EventBus.$emit('messageA', 'Hello from Component A'); } } } </script>
子組件B:
<template> <div> <h2> 組件B:</h2> <el-button @click="sendMessage">發(fā)送消息給A</el-button> <h4> 從A獲取的消息:</h4> <div style="color: red;">{{ message }}</div> </div> </template> <script> import { EventBus } from '@/eventbus/index.js'; export default { data() { return { message: '' }; }, mounted() { EventBus.$on('messageA', (msg) => { this.message = msg; }); }, methods: { sendMessage() { EventBus.$emit('messageB', 'Hello from Component B'); } } } </script>
eventbus/index.js:
import Vue from ‘vue’;
export const EventBus = new Vue();
EventBus的基本使用方法
List item初始化EventBus:首先需要?jiǎng)?chuàng)建一個(gè)EventBus實(shí)例。可以通過創(chuàng)建一個(gè)新的.js文件(如event-bus.js),然后導(dǎo)入Vue并實(shí)例化一個(gè)新的Vue對(duì)象來創(chuàng)建EventBus?;蛘撸梢灾苯釉陧?xiàng)目的main.js文件中初始化EventBus。
發(fā)送事件:要向EventBus發(fā)送事件,可以使用e m i t 方 法 。 例 如 , t h i s . emit方法。例如,this.emit方法。例如,this.EventBus.$emit(‘eventName’, payload),其中eventName是事件名,payload是要傳遞的參數(shù)。
監(jiān)聽事件:要在組件中監(jiān)聽EventBus上的事件,可以使用o n 方 法 。 例 如 , t h i s . on方法。例如,this.on方法。例如,this.EventBus.$on(‘eventName’, callback),其中eventName是事件名,callback是處理事件的函數(shù)。
監(jiān)聽:如果需要在組件銷毀時(shí)移除事件監(jiān)聽,可以使用o f f 方 法 。 例 如 , t h i s . off方法。例如,this.off方法。例如,this.EventBus.$off(‘eventName’, callback)。
注意事項(xiàng):雖然EventBus提供了一種簡(jiǎn)單的方式來實(shí)現(xiàn)組件間通信,但過度使用可能會(huì)導(dǎo)致代碼難以維護(hù)。因此,對(duì)于更復(fù)雜的應(yīng)用,建議使用Vuex作為狀態(tài)管理解決方案。
總的來說,通過以上步驟,可以在Vue項(xiàng)目中使用EventBus來實(shí)現(xiàn)不同組件之間的通信。
到此這篇關(guān)于vue中EventBus的使用教程詳解的文章就介紹到這了,更多相關(guān)vue EventBus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作實(shí)例
這篇文章主要介紹了Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作,結(jié)合實(shí)例形式分析了vue基于axios庫post傳送表單json格式數(shù)據(jù)相關(guān)操作實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2023-06-06如何利用vue實(shí)現(xiàn)css過渡和動(dòng)畫
過渡Vue在插入、更新或者移除 DOM 時(shí),提供多種不同方式的應(yīng)用過渡效果這篇文章主要給大家介紹了關(guān)于如何利用vue實(shí)現(xiàn)css過渡和動(dòng)畫的相關(guān)資料,需要的朋友可以參考下2021-11-11Vue3使用Vue Router實(shí)現(xiàn)前端路由控制
在現(xiàn)代Web應(yīng)用中,前端路由控制是非常重要的一部分,它可以幫助我們將不同的頁面內(nèi)容展示給用戶,同時(shí)保持用戶在瀏覽不同頁面時(shí)的連貫性,本文將介紹如何使用Vue Router來實(shí)現(xiàn)前端路由控制,需要的朋友可以參考下2024-10-10vue關(guān)于this.$refs.tabs.refreshs()刷新組件方式
這篇文章主要介紹了vue關(guān)于this.$refs.tabs.refreshs()刷新組件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Vue+penlayers實(shí)現(xiàn)多邊形繪制及展示
這篇文章主要為大家詳細(xì)介紹了Vue+penlayers實(shí)現(xiàn)多邊形繪制及展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12vue實(shí)現(xiàn)子路由調(diào)用父路由的方法
這篇文章主要介紹了vue實(shí)現(xiàn)子路由調(diào)用父路由的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06vue中el-table兩個(gè)表尾合計(jì)行聯(lián)動(dòng)同步滾動(dòng)條實(shí)例代碼
項(xiàng)目開發(fā)中遇到一個(gè)比較兩個(gè)form差異的需求,但當(dāng)item過多就需要滾動(dòng)條,下面這篇文章主要給大家介紹了關(guān)于vue中el-table兩個(gè)表尾合計(jì)行聯(lián)動(dòng)同步滾動(dòng)條的相關(guān)資料,需要的朋友可以參考下2022-05-05