JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="counter-event-example">
<p>{{ total }}</p>
<button-counter v-on:incrementOne="incrementTotal"></button-counter>
<button-counter v-on:incrementOne="incrementTotal"></button-counter>
</div>
JavaScript
Vue.component('button-counter', {
template: '<button v-on:click="incrementOne">{{ counter }}</button>',
data: function () {
return {
counter: 0
}
},
methods: {
incrementOne: function () {
this.counter += 1
this.$emit('incrementOne')
}
},
})
new Vue({
el: '#counter-event-example',
data: {
total: 0
},
methods: {
incrementTotal: function () {
this.total += 1
}
}
})