Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue event modifiers prevent vs. stop

Tags:

Reading the Vue documentation about events, they mention the event modifiers like prevent or stop. They mention that on stop: <!-- the click event's propagation will be stopped -->. I am assuming this will stop the event from bubbling. What about prevent. What does it exactly do? I am assuming it will prevent the event from being triggered twice (on a double click for example). Are these assumptions correct? I just couldn't find more specific info on the web.

What I read:

  • https://vuejs.org/v2/guide/events.html
  • How to prevent/stop propagation of default event (click) in directive (vue 2.x)
like image 823
lukas_o Avatar asked Oct 27 '17 06:10

lukas_o


People also ask

What is prevent in Vue?

Prevent default behaviorTo prevent an event's default behavior, we can call the . prevent modifier in Vue, which calls the native event. preventDefault() method. This series of steps prevents the event from reloading the page after the form is submitted.

How do you stop event propagation in Vue?

preventDefault() or event. stopPropagation() inside event handlers. It is always better to have your methods deal purely with data logic instead of having to deal with DOM event details. Vue provides us with some event modifiers for the v-on.

Which of the following event modifiers will prevent all clicks?

self will prevent all clicks while @click. self. prevent will only prevent clicks on the element itself.

Which event modifier is used for only prevent click on the element itself?

self. prevent will only prevent clicks on the element itself. Unlike the other modifiers, which are exclusive to native DOM events, the . once modifier can also be used on component events.


2 Answers

.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

like image 194
Quoc-Anh Nguyen Avatar answered Sep 18 '22 03:09

Quoc-Anh Nguyen


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>

Here is how it works
On the first div element, (click) event for (div) element is handled by (div) and by the children of (div) because we did not stop propagation.
So once you click on button, the click event for button is triggered first then bubbling is done by moving on the ancestors of button.
The target is the element that handles the event while the currentTarget may be element that handles the event or ancestors of the element.
For this reason when you click on button on the first (div), the click event triggers twice due to handling click event of the parent.

like image 27
Abdullrahman Qutaiba Avatar answered Sep 19 '22 03:09

Abdullrahman Qutaiba