Vue - Method Event Handlers

0 votes
added Oct 25, 2019 in Vue by LC Marshal Captain (25,790 points)

 Keeping value of v-on attribute isn’t always feasible especially in the event, where event handlers gets more complex. That's when v-on hang onto method. 

<div id="hallo">
  <!-- `hallo` is the name of a method defined below -->
  <button v-on:click="halloclick">Hallo</button>

var hallo = new Vue({
  el: '#hallo',
  data: {
    name: 'Hallo LC'
  // methods to be defined under `methods` object
  methods: {
    halloclick: function (event) {
      // `this` inside methods points to the Vue instance
      alert('Hello ' + + '!')
      // `event` is the native DOM event
      if (event) {

// you can invoke methods in JS as well
hallo.halloclick() // => 'Hallo LC'
</script> - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.