vue使用$emit時,父組件無法監聽到子組件的事件的原因是$emit傳入的事件名稱只能使用小寫,不能使用大寫的駝峰規則命名
<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);//無效,不能使用大寫的駝峰規則命名 } }, }) new Vue({ el: '#counter-event-example', data: { total: '點擊下面的按鈕' }, methods: { incrementTotal: function (b) { this.total = b; } } }) </script>以上這篇vue使用$emit時,父組件無法監聽到子組件的事件實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答