vue custom event

by oktaviardi pratama

HTML

<div id="counter-event-example">
  <p>{{ total }}</p>
  <button-counter v-on:total="onTotal"></button-counter>
</div>

JavaScript

Vue.component('button-counter', {
  template: `<button
v-on:click="toggle">
{{ counter }}</button>`,
  data: function () {
    return {
      counter: 0
    }
  },
  methods: {
    toggle: function () {
      this.counter += 1
      alert()
      this.$emit('total')
    }
  },
})
new Vue({
  el: '#counter-event-example',
  data: {
    total: 0
  },
  methods: {
    onTotal: function () {
      this.total += 1
      alert()
    }
  }
})