Vue自定義事件(詳解)
前面的話(huà)
父組件使用props傳遞數(shù)據(jù)給子組件,子組件怎么跟父組件通信呢?這時(shí),Vue的自定義事件就派上用場(chǎng)了。本文將詳細(xì)介紹Vue自定義事件
事件綁定
每個(gè) Vue 實(shí)例都實(shí)現(xiàn)了事件接口 (Events interface),即
使用 $on(eventName) 監(jiān)聽(tīng)事件 使用 $emit(eventName) 觸發(fā)事件
[注意]Vue 的事件系統(tǒng)分離自瀏覽器的EventTarget API。盡管它們的運(yùn)行類(lèi)似,但是 $on 和 $emit 不是addEventListener 和 dispatchEvent 的別名
另外,父組件可以在使用子組件的地方直接用 v-on 來(lái)監(jiān)聽(tīng)子組件觸發(fā)的事件
[注意]不能用 $on 偵聽(tīng)子組件拋出的事件,而必須在模板里直接用 v-on 綁定
<div id="example"> <parent></parent> </div>
<script> var childNode = { template: `<button @click="incrementCounter">{{ counter }}</button>`, data(){ return { counter: 0 } }, methods:{ incrementCounter(){ this.counter ++; this.$emit('increment'); } }, } var parentNode = { template: ` <div class="parent"> <p>{{total}}</p> <child @increment="incrementTotal"></child> <child @increment="incrementTotal"></child> </div> `, components: { 'child': childNode }, data(){ return { 'total':0 } }, methods:{ incrementTotal(){ this.total ++; } } }; // 創(chuàng)建根實(shí)例 new Vue({ el: '#example', components: { 'parent': parentNode } }) </script>
命名約定
自定義事件的命名約定與組件注冊(cè)及props的命名約定都不相同,由于自定義事件實(shí)質(zhì)上也是屬于HTML的屬性,所以其在HTML模板中,最好使用中劃線形式
<child @pass-data="getData"></child>
而子組件中觸發(fā)事件時(shí),同樣使用中劃線形式
this.$emit('pass-data',this.childMsg)
數(shù)據(jù)傳遞
子組件通過(guò)$emit可以觸發(fā)事件,第一個(gè)參數(shù)為要觸發(fā)的事件,第二個(gè)事件為要傳遞的數(shù)據(jù)
this.$emit('pass-data',this.childMsg)
父組件通過(guò)$on監(jiān)聽(tīng)事件,事件處理函數(shù)的參數(shù)則為接收的數(shù)據(jù)
getData(value){ this.msg = value; }
<div id="example"> <parent></parent> </div>
<script> var childNode = { template: ` <div class="child"> <div> <span>子組件數(shù)據(jù)</span> <input v-model="childMsg" @input="data"> </div> <p>{{childMsg}}</p> </div> `, data(){ return{ childMsg:'' } }, methods:{ data(){ this.$emit('pass-data',this.childMsg) } } } var parentNode = { template: ` <div class="parent"> <div> <span>父組件數(shù)據(jù)</span> <input v-model="msg"> </div> <p>{{msg}}</p> <child @pass-data="getData"></child> </div> `, components: { 'child': childNode }, data(){ return { 'msg':'match' } }, methods:{ getData(value){ this.msg = value; } } }; // 創(chuàng)建根實(shí)例 new Vue({ el: '#example', components: { 'parent': parentNode } }) </script>
sync修飾符
在一些情況下,可能會(huì)需要對(duì)一個(gè) prop 進(jìn)行雙向綁定。事實(shí)上,這正是Vue1.x中的 .sync修飾符所提供的功能。當(dāng)一個(gè)子組件改變了一個(gè) prop 的值時(shí),這個(gè)變化也會(huì)同步到父組件中所綁定的值。這很方便,但也會(huì)導(dǎo)致問(wèn)題,因?yàn)樗茐牧藛蜗驍?shù)據(jù)流的假設(shè)。由于子組件改變 prop 的代碼和普通的狀態(tài)改動(dòng)代碼毫無(wú)區(qū)別,當(dāng)光看子組件的代碼時(shí),完全不知道它何時(shí)悄悄地改變了父組件的狀態(tài)。這在 debug 復(fù)雜結(jié)構(gòu)的應(yīng)用時(shí)會(huì)帶來(lái)很高的維護(hù)成本,上面所說(shuō)的正是在 2.0 中移除 .sync 的理由
從 2.3.0 起重新引入了 .sync 修飾符,但是這次它只是作為一個(gè)編譯時(shí)的語(yǔ)法糖存在。它會(huì)被擴(kuò)展為一個(gè)自動(dòng)更新父組件屬性的 v-on 偵聽(tīng)器
<comp :foo.sync="bar"></comp>
會(huì)被擴(kuò)展為:
<comp :foo="bar" @update:foo="val => bar = val"></comp>
當(dāng)子組件需要更新 foo 的值時(shí),它需要顯式地觸發(fā)一個(gè)更新事件:
this.$emit('update:foo', newValue)
因此,可以使用.sync來(lái)簡(jiǎn)化自定義事件的操作,實(shí)現(xiàn)子組件向父組件的數(shù)據(jù)傳遞
<div id="example"> <parent></parent> </div> <script src="https://unpkg.com/vue"></script> <script> var childNode = { template: ` <div class="child"> <div>子組件數(shù)據(jù):{{childMsg}}</div> <input v-model="childMsg"> <button @click=add >+1</button> </div> `, data(){ return{ childMsg: 0 } }, methods:{ add(){ this.childMsg++; this.$emit('update:foo',this.childMsg); } } }; var parentNode = { template: ` <div class="parent"> <p>父組件數(shù)據(jù):{{msg}}</p> <child :foo.sync="msg"></child> </div> `, components: { 'child': childNode }, data(){ return { 'msg':0 } } }; // 創(chuàng)建根實(shí)例 new Vue({ el: '#example', components: { 'parent': parentNode } }) </script>
以上這篇Vue自定義事件(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目的創(chuàng)建的步驟(圖文教程)
本文主要介紹了vue項(xiàng)目的創(chuàng)建的步驟(圖文教程),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03vue3自定義組件之v-model實(shí)現(xiàn)父子組件雙向綁定
這篇文章主要介紹了vue3自定義組件之v-model實(shí)現(xiàn)父子組件雙向綁定方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue使用 better-scroll的參數(shù)和方法詳解
這篇文章主要介紹了vue使用 better-scroll的參數(shù)和方法詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01el-table表頭添加勾選框的實(shí)現(xiàn)示例
本文主要介紹了el-table表頭添加勾選框的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01vue中使用Echarts?map圖實(shí)現(xiàn)下鉆至縣級(jí)的思路詳解
這篇文章主要介紹了vue中使用Echarts?map圖實(shí)現(xiàn)下鉆至縣級(jí),需要注意的是,因?yàn)槲沂侵苯訌?vue-cli2?直接跳到?vue-cli4?,還奇怪怎么讀取不到JSON,查找后才知道?vue-cli3?往后的項(xiàng)目基礎(chǔ)架構(gòu)對(duì)比舊版本有些區(qū)別,感興趣的朋友跟隨小編一起看看吧2022-01-01VUE對(duì)Storage的過(guò)期時(shí)間設(shè)置,及增刪改查方式
這篇文章主要介紹了VUE對(duì)Storage的過(guò)期時(shí)間設(shè)置,及增刪改查方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02