vue使用$emit時(shí),父組件無法監(jiān)聽到子組件的事件的原因是$emit傳入的事件名稱只能使用小寫,不能使用大寫的駝峰規(guī)則命名
<div id="counter-event-example"> <p>{{ total }}</p> <button-counter v-on:ee="incrementTotal"></button-counter> <button-counter v-on:eEvent="incrementTotal"></button-counter> <child ref="cmpSelect" v-on:ee="incrementTotal" option-api-url="/api/admin/cms/cmsCategory/getOptions.do"></child> </div> <script> Vue.component('button-counter', { template: '<button v-on:click="increment">{{ counter }}</button>', data: function () { return { counter: 0 } }, methods: { increment: function () { this.counter += 1 this.$emit('ee', this.counter);//有效 this.$emit('eEvent', this.counter);//無效,不能使用大寫的駝峰規(guī)則命名 } }, }) new Vue({ el: '#counter-event-example', data: { total: '點(diǎn)擊下面的按鈕' }, methods: { incrementTotal: function (b) { this.total = b; } } }) </script>以上這篇vue使用$emit時(shí),父組件無法監(jiān)聽到子組件的事件實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持錯(cuò)新站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選