這篇文章主要介紹“Vue中如何實(shí)現(xiàn)非父子組件通信”,在日常操作中,相信很多人在Vue中如何實(shí)現(xiàn)非父子組件通信問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Vue中如何實(shí)現(xiàn)非父子組件通信”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
目前成都創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、鎮(zhèn)雄網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
組件是Vue核心的一塊內(nèi)容,組件之間的通信也是很基本的開發(fā)需求。組件通信又包括父組件向子組件傳數(shù)據(jù),子組件向父組件傳數(shù)據(jù),非父子組件間的通信。前兩種通信Vue的文檔都說的很清楚,但是第三種文檔上確只有下面的幾句
具體如何去實(shí)現(xiàn)卻沒有很詳細(xì)的說明,于是自己試著進(jìn)行了實(shí)現(xiàn)。先看下簡單的通信效果:
就是點(diǎn)擊了一個(gè)組件,另一個(gè)組件的數(shù)字遞加。
html如下:
<div id="app"> <component-a></component-a> <component-b></component-b> </div>
再來看一下如何實(shí)現(xiàn)每一個(gè)組件:
var bus = new Vue() //首先建立一個(gè)空的Vue實(shí)例作為事件的中轉(zhuǎn) Vue.component('component-a',{ template: `<div><button @click="incrementB">{{masgA}}</button></div>`, //添加點(diǎn)擊事件incrementB ,因?yàn)辄c(diǎn)擊A需要增加B data () { return { masgA : 0 } }, methods: { incrementB: function () { //增加B的事件 bus.$emit('incrementB') //中轉(zhuǎn)站bus 觸發(fā)incrementB事件 } }, mounted: function () { var _this = this bus.$on('incrementA',function(){ //中轉(zhuǎn)站bus自定義increamentA事件用來增加msgA,這個(gè)事件最終由組件B進(jìn)行觸發(fā) _this.masgA ++ }) //bus.$on('incrementA',()=>{ //這里也可以用箭頭函數(shù),就不會有_this這個(gè)變量了,因?yàn)榧^函數(shù)不會改變this指向 // this.masgA ++ //}) } })
從上面的代碼可以看出真正的改變方法是通過bus里注冊監(jiān)聽事件來實(shí)現(xiàn)的,同理代component-b也是一樣
Vue.component('component-b',{ template: `<div><button @click="incrementA">{{masgB}}</button></div>`, data () { return { masgB : 0 } }, methods: { incrementA: function () { bus.$emit('incrementA') } }, mounted: function(){ bus.$on('incrementB',() => { this.masgB ++ }) } })
完整代碼如下,可直接復(fù)制運(yùn)行
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>非父子組件通信</title> </head> <body> <div id="app"> <component-a></component-a> <component-b></component-b> </div> <script src="https://unpkg.com/vue/dist/vue.js"></script> </body> <script> var bus = new Vue() //首先建立一個(gè)空的Vue實(shí)例作為事件的中轉(zhuǎn) Vue.component('component-a',{ template: `<div><button @click="incrementB">{{masgA}}</button></div>`, //添加點(diǎn)擊事件 data () { return { masgA : 0 } }, methods: { incrementB: function () { bus.$emit('incrementB') } }, mounted: function () { var _this = this bus.$on('incrementA',function(){ _this.masgA ++ }) bus.$on('incrementA',()=>{ this.masgA ++ }) } }) Vue.component('component-b',{ template: `<div><button @click="incrementA">{{masgB}}</button></div>`, data () { return { masgB : 0 } }, methods: { incrementA: function () { bus.$emit('incrementA') } }, mounted: function(){ bus.$on('incrementB',() => { this.masgB ++ }) } }) var vm = new Vue({ el: "#app" }) </script>
同時(shí)也可以看出這么做僅有兩個(gè)組件就有些麻煩,事件的流向不是很清晰,所以在出現(xiàn)復(fù)雜的場景時(shí)需要使用VueX進(jìn)行管理。
到此,關(guān)于“Vue中如何實(shí)現(xiàn)非父子組件通信”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
分享文章:Vue中如何實(shí)現(xiàn)非父子組件通信
網(wǎng)站地址:http://jinyejixie.com/article34/pgipse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、用戶體驗(yàn)、網(wǎng)站制作、服務(wù)器托管、企業(yè)建站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)