Vue event modifiers prevent vs. stop Vue event modifiers prevent vs. stop vue.js vue.js

Vue event modifiers prevent vs. stop


.prevent or event.preventDefault() – It stops the browsers default behaviour (A example is reload when you hit <button type="submit"> in <form>)

.stop or event.stopPropagation() – It prevents the event from propagating (or “bubbling up”) the DOM

.once - The event will be triggered at most once


Here is a practical example in VueJs 2:

var stopEx = new Vue({    el: '#stop-example',    methods: {        elClick: function(event) {            alert("Click from "+event.target.tagName+"\nCurrent Target: "+event.currentTarget.tagName);        }    }})
#stop-example > div {        max-width: 300px;        min-height: 150px;        border: 2px solid black;    }
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script><div id="stop-example">    <h3>without stop propagation</h3>    <div @click="elClick($event)">        <button @click="elClick($event)">Click Me</button>    </div>    <h3>with stop propagation</h3>    <div @click="elClick($event)">        <button @click.stop="elClick($event)">Click Me</button>    </div></div>