Vue - Method Event Handlers

0 votes
369 views
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. 

  1. <div id="hallo">
  2. <!-- `hallo` is the name of a method defined below -->
  3. <button v-on:click="halloclick">Hallo</button>
  4. </div>
  5.  
  6. <script>
  7. var hallo = new Vue({
  8. el: '#hallo',
  9. data: {
  10. name: 'Hallo LC'
  11. },
  12. // methods to be defined under `methods` object
  13. methods: {
  14. halloclick: function (event) {
  15. // `this` inside methods points to the Vue instance
  16. alert('Hello ' + this.name + '!')
  17. // `event` is the native DOM event
  18. if (event) {
  19. alert(event.target.tagName)
  20. }
  21. }
  22. }
  23. })
  24.  
  25. // you can invoke methods in JS as well
  26. hallo.halloclick() // => 'Hallo LC'
  27. </script>

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...