Vue中this.$emit和this.$on的使用
1、解決第一次監(jiān)聽(tīng)不到數(shù)據(jù)的方法
- this.$nextTick()
this.$nextTick(function () { this.$bus.$emit("yulan", row); })
- setTimeout
setTimeout(() => { //row 代表要傳遞的參數(shù)數(shù)據(jù) this.$bus.$emit("yulan", row); }, 100);
2、子組件傳值給父組件
子組件中使用$emit觸發(fā)事件的值傳出去,父組件通過(guò)事件監(jiān)聽(tīng),獲取數(shù)據(jù);
兩種情況
由子組件中操作何時(shí)傳值
(解決:在子組件中定義一個(gè)click事件觸發(fā)自定義事件$emit,在父組件監(jiān)聽(tīng))
// 父組件:parent.vue <template> <div> <child @childevent='wathChildEvent'></child> <div>子組件的數(shù)據(jù)為:{{msg}}</div> </div> </template> <script> import child from "./child"; export default { data(){ return{ msg:"" } }, components:{ child }, methods:{ wathChildEvent:function(val){//直接監(jiān)聽(tīng) 又子組件觸發(fā)的事件,參數(shù)為子組件的傳來(lái)的數(shù)據(jù) console.log(val); //子組件的數(shù)據(jù),將有子組件操作觸發(fā)傳給父組件 this.msg = val; } } } </script> // 子組件:child.vue <template> <div><input type="button" value="子組件觸發(fā)" @click="target"></div> </template> <script> export default { data(){ return { texts:'這是子組件的數(shù)據(jù),將有子組件操作觸發(fā)傳給父組件' } }, methods:{ target:function(){ //有子組件的事件觸發(fā) 自定義事件childevent this.$emit('childevent',this.texts);//觸發(fā)一個(gè)在子組件中聲明的事件 childEvnet } }, } </script>
由父組件中操作何時(shí)傳值
(解決:在父組件中第一一個(gè)click點(diǎn)擊事件,在組件中通過(guò)refs獲取實(shí)例方法來(lái)直接觸發(fā)事件,然后在父組件中監(jiān)聽(tīng))
// 父組件:parent.vue <template> <div> <child @childevent='wathChildEvent' ref="childcomp"></child> <input type="button" @click="parentEnvet" value="父組件觸發(fā)" /> <div>子組件的數(shù)據(jù)為:{{msg}}</div> </div> </template> <script> import child from "./child"; export default { data(){ return{ msg:"" } }, components:{ child }, methods:{ wathChildEvent:function(val){//直接監(jiān)聽(tīng) 又子組件觸發(fā)的事件,參數(shù)為子組件的傳來(lái)的數(shù)據(jù) console.log(val);//子組件的數(shù)據(jù),將有子組件操作觸發(fā)傳給父組件 this.msg = val; }, parentEnvet:function(){ this.$refs['childcomp'].target(); //通過(guò)refs屬性獲取子組件實(shí)例,又父組件操作子組件的方法觸發(fā)事件$meit } } } </script> // 子組件:child.vue <template> <div> dothing </div> </template> <script> export default { data(){ return { texts:'這是子組件的數(shù)據(jù),將有子組件操作觸發(fā)傳給父組件' } }, methods:{ target:function(){ //又子組件的事件觸發(fā) 自定義事件childevent this.$emit('childevent',this.texts);//觸發(fā)一個(gè)在子組件中聲明的事件 childEvnet } }, } </script>
3、兄弟組件間傳值
- 傳遞數(shù)據(jù)方–發(fā)射事件
通過(guò)事件傳遞$emit(方法名,傳遞的數(shù)據(jù))
- 接收數(shù)據(jù)方–監(jiān)聽(tīng)事件
在mounted()中觸發(fā)$on(方法名,callback(接收數(shù)據(jù)的數(shù)據(jù)))
// 建立一個(gè)空的Vue實(shí)例,將通信事件掛載在該實(shí)例上 // emptyVue.js import Vue from 'vue' export default new Vue() // 兄弟組件a:childa.vue <template> <div> <span>A組件->{{msg}}</span> <input type="button" value="把a(bǔ)組件數(shù)據(jù)傳給b" @click ="send"> </div> </template> <script> import vmson from "./emptyVue" export default { data(){ return { msg:"我是a組件的數(shù)據(jù)" } }, methods:{ send:function(){ vmson.$emit("aevent",this.msg) } } } </script> // 兄弟組件b:childb.vue <template> <div> <span>b組件,a傳的的數(shù)據(jù)為->{{msg}}</span> </div> </template> <script> import vmson from "./emptyVue" export default { data(){ return { msg:"" } }, mounted(){ vmson.$on("aevent",(val)=>{//監(jiān)聽(tīng)事件aevent,回調(diào)函數(shù)要使用箭頭函數(shù); console.log(val);//打印結(jié)果:我是a組件的數(shù)據(jù) this.msg = val; }) } } </script> // 父組件:parent.vue <template> <div> <childa><childa> <childb></childb> </div> </template> <script> import childa from "./childa"; import childb from "./childb"; export default { data(){ return{ msg:"" } }, components:{ childa, childb }, } </script>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中實(shí)現(xiàn)動(dòng)態(tài)右鍵菜單的示例代碼
在前端開(kāi)發(fā)中,實(shí)現(xiàn)自定義右鍵菜單能夠?yàn)橛脩籼峁└喙δ苓x項(xiàng),本文就來(lái)介紹了Vue中實(shí)現(xiàn)動(dòng)態(tài)右鍵菜單的示例代碼,感興趣的可以了解一下2024-11-11vue通過(guò)路由實(shí)現(xiàn)頁(yè)面刷新的方法
本篇文章主要介紹了vue通過(guò)路由實(shí)現(xiàn)頁(yè)面刷新的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Vue+Vant實(shí)現(xiàn)7天日歷展示并在切換日期時(shí)實(shí)時(shí)變換功能
本文介紹了如何利用Vue和Vant框架結(jié)合moment.js插件來(lái)實(shí)現(xiàn)一個(gè)7天日歷展示功能,在這個(gè)功能中,用戶可以在切換日期時(shí)看到界面的實(shí)時(shí)變化,此外,文章還提供了代碼實(shí)現(xiàn)和效果測(cè)試的詳細(xì)步驟,幫助開(kāi)發(fā)者能夠順利完成類似的項(xiàng)目開(kāi)發(fā)2024-10-10ElementUI中<el-form>標(biāo)簽中ref、:model、:rules的作用淺析
Element官方文檔中寫到,model是表單數(shù)據(jù)對(duì)象,rules是表單驗(yàn)證規(guī)則,下面這篇文章主要給大家介紹了關(guān)于ElementUI中<el-form>標(biāo)簽中ref、:model、:rules作用的相關(guān)資料,需要的朋友可以參考下2023-01-01vue動(dòng)態(tài)路由匹配和路由懶加載多種方法詳解
這篇文章主要介紹了vue動(dòng)態(tài)路由匹配和路由懶加載,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06Vue自定義省市區(qū)三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Vue自定義省市區(qū)三級(jí)聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03