關(guān)于vue.js中this.$emit的理解使用
一、每個(gè) Vue 實(shí)例都實(shí)現(xiàn)了事件接口
即:
1、使用 $on(eventName) 監(jiān)聽(tīng)事件
2、使用 $emit(eventName, optionalPayload) 觸發(fā)事件
二、注意事項(xiàng)
1、父組件可以在使用子組件的地方直接用 v-on 來(lái)監(jiān)聽(tīng)子組件觸發(fā)的事件
2、不能用 $on 監(jiān)聽(tīng)子組件釋放的事件,而必須在模板里直接用 v-on 綁定
三、例子及說(shuō)明
1、父組件代碼及說(shuō)明
<template>
? <div>
? ? <p>{{ total }}</p>
? ? <my-button4 @increment1="incrementTotal1"></my-button4> ? ? <!--自定義方法increment1監(jiān)聽(tīng)子組件觸發(fā)情況-->
? ? <my-button4 @increment2="incrementTotal2"></my-button4> ? ? <!--自定義方法increment2監(jiān)聽(tīng)子組件觸發(fā)情況-->
? </div>
</template>
<script>
? import myButton4 from './components/myButton4.vue'
? export default{
? ? data(){
? ? ? return{
? ? ? ? ? total:0
? ? ? }
? ? },
? ? methods:{
? ? ? incrementTotal1: function () { ? ? ? ? ? ? ? ? ? ? /*事件incrementTotal觸發(fā)*/
? ? ? ? this.total += 1
? ? ? },
? ? ? incrementTotal2: function () { ? ? ? ? ? ? ? ? ? ?/*事件incrementTota2觸發(fā)*/
? ? ? ? this.total += 2
? ? ? }
? ? },
? ? components:{ ? ? ? ? ? ? ? ? ? ? ? ?/*子組件的實(shí)例,要盡量放在最后,不然會(huì)出現(xiàn)一些不必要的問(wèn)題*/
? ? ? myButton4
? ? }
? }
</script>2、子組件代碼及說(shuō)明
<template>
? ? ? <button @click="incrementCounter">{{counter}}</button> <!--在子組件中創(chuàng)建一個(gè)按鈕,創(chuàng)建點(diǎn)擊事件-->
</template>
<script>
? ?export default{
? ? ?data(){
? ? ? ?return{
? ? ? ? ?counter: 0
? ? ? ?}
? ? ?},
? ? ?methods: {
? ? ? ?incrementCounter: function (){
? ? ? ? ?this.counter += 1
? ? ? ? ?this.$emit('increment1') ? ? ? ?/*觸發(fā)自定義事件increment1,也就是父組件中的incrementTotal1事件*/
? ? ? ? ?this.$emit('increment2') ? ? ? ?/*觸發(fā)自定義事件increment2,也就是父組件中的incrementTotal2事件*/
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/*這兩個(gè)事件一次只會(huì)觸發(fā)一個(gè),為什么呢?很簡(jiǎn)單,因?yàn)槊看沃粏螕粢粋€(gè)按鈕*/
? ? ? ?}
? ? ?}
? ?}
</script>3、運(yùn)行截圖
A、開(kāi)始截圖:

B、點(diǎn)擊第一個(gè)按鈕截圖(+1)

C、點(diǎn)擊第二個(gè)按鈕截圖(+2)

四、總說(shuō)明
1、首先看子組件件,按鈕中給其綁定了方法:incrementCounter;
2、點(diǎn)擊button時(shí)會(huì)執(zhí)行函數(shù) incrementCounter,increment中有 this.$emit(‘increment1)和this.$emit(‘increment2),看點(diǎn)擊的是哪個(gè)按鈕就執(zhí)行哪個(gè);
3、當(dāng)incrementCounter執(zhí)行時(shí),就會(huì)觸發(fā)自定函數(shù)increment1(點(diǎn)擊第一個(gè)按鈕的時(shí)候)或者increment(點(diǎn)擊第二個(gè)按鈕的時(shí)候),也就是incrementTotal1或者incrementTotal2函數(shù);
到此這篇關(guān)于關(guān)于vue.js中this.$emit的理解使用的文章就介紹到這了,更多相關(guān)vue.js this.$emit內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue2實(shí)時(shí)監(jiān)聽(tīng)表單變化的示例講解
今天小編就為大家分享一篇Vue2實(shí)時(shí)監(jiān)聽(tīng)表單變化的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue中多個(gè)文件下載實(shí)現(xiàn)打包壓縮下載示例
這篇文章主要為大家介紹了vue中多個(gè)文件下載實(shí)現(xiàn)打包壓縮下載的發(fā)發(fā)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
vue項(xiàng)目實(shí)現(xiàn)背景顏色以及下劃線從左到右漸變動(dòng)畫(huà)效果
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)背景顏色以及下劃線從左到右漸變動(dòng)畫(huà)效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
electron-vue+electron-updater實(shí)現(xiàn)自動(dòng)更新(步驟源碼)
這篇文章主要介紹了electron-vue+electron-updater實(shí)現(xiàn)自動(dòng)更新,步驟源碼包括autoUpdater.js操控更新js文件,main.js也就是package.json內(nèi)的main指向的js文件,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
Vue中使用vue-count-to(數(shù)字滾動(dòng)插件)詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于Vue中使用vue-count-to(數(shù)字滾動(dòng)插件)的相關(guān)資料,最近需要開(kāi)發(fā)一個(gè)數(shù)字滾動(dòng)效果,在網(wǎng)上找到一個(gè)關(guān)于vue-countTo的插件,覺(jué)得這個(gè)插件還不錯(cuò),需要的朋友可以參考下2023-09-09

