Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs - automatic event.preventDefault()

right now, I need to add event.preventDefault() to all of my click events in my Vue.js buttons:

<button class="btn btn-primary" v-on:click="someAction($event)">Action</button></p>

methods: {
 someAction (e) {
   e.preventDefault()
   console.log('in some action')
 },
}

Does anyone know of a way have event.preventDefeault() be the default setting? Right now it's very annoying to have to include the event.preventDefault() in every click event.

Thanks in advance!

like image 782
John Grayson Avatar asked Mar 07 '18 05:03

John Grayson


People also ask

How do I stop Vue default form?

The other way we can achieve preventing the default form submission behaviour is by attaching a submit event with the prevent modifier to the starting form tag in the template. This will prevent the page from reloading when the submit button is pressed as well.

How do I stop a Vue event propagate?

Specifically, we can use stop event modifier. . stop will stop the event propagation.

What is click prevent in Vue JS?

prevent is a modifier for v-on:click . Another handy modifier is . self , which tells Vue to only evaluate v-on:click if the element itself is clicked, as opposed to one of its children. For example, Vue only calls the below v-on:click handler when you click on the outer div , not the inner div .08-Jul-2020.


1 Answers

You can use prevent modifier:

@click.prevent="YourMethod"

You can look event modifier for more information.

like image 160
Bhojendra Rauniyar Avatar answered Oct 19 '22 11:10

Bhojendra Rauniyar