Skip to content

Instantly share code, notes, and snippets.

@ashx3s
Last active November 23, 2021 17:24
Show Gist options
  • Select an option

  • Save ashx3s/74543137780c1958f01e9b470a3cb13b to your computer and use it in GitHub Desktop.

Select an option

Save ashx3s/74543137780c1958f01e9b470a3cb13b to your computer and use it in GitHub Desktop.
Vue v-on

Vue v-on syntax

  • The v-on directive follows this logic:
v-on:event="method"
  • You can also use the shorthand syntax like
@event="method"

Button Example

This button will add to a counter when it is clicked

<template>
<button @click="counterAdd">Add 1</button>
</template>
  • An accompanying method could look like this
data () {
  counter: 0
},
methods: {
  counterAdd: function(event) {
    return this.counter += 1
  }
}

Event modifiers

You can add event modifiers to manipulate how v-on will work. Here are a couple examples taken from the above noted mozilla documentation.

  • .prevent : Will stop the page from reloading when a form has been submitted

  • .once : Will listen for the event to be triggered once, and then will ignore it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment