Here is how to master this problem.
Say you have a parent element and some child elements.
1.(1st case) You want the parent click to do not affect the child clicks. Just put at the parent element the .self
modifier:
<div class="parent" @click.self="parent"> <!-- .self modifier -->
<span class="child" @click="child1">Child1</span>
<span class="child" @click="child2">Child2</span>
<span class="child" @click="child3">Child3</span>
</div>
See it in action here
note: if you remove the .self
when you click a child, the parent event will fire too.
2.(2nd case) You have the below code:
<div @click="parent">
Click to activate
<i class="fa fa-trash" title="delete this" @click="delete_clicked"></i>
</div>
The problem is:
- When you click the icon element the parent click will fire. (you don't want this)
- You can NOT use the 1st solution because you want to fire the parent event even if the text "Click to activate" gets clicked.
The solution to this is to put the .stop
modifier to the icon element so the parent event will not fire.
See it in action here