Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent/stop click propagation in vuejs

I have a recursive list (tree) and each element has a @click="sayHello(el.id)". Now the problem is, since it is a nested list like:

list-element-0-01
├──list-el-1-01
│   └──list-el-2-01
└──list-el-1-02
    └──list-el-2-02

If I click the element: list-el-2-01 then I get the output of

 > "list-el-2-01"
 > "list-el-1-01"
 > "list-el-0-01"

In exact that order. I mean I get it, looking at the html:

<ul>
  <li @click="sayHello('list-el-0-01')"> one-one

    <ul>
      <li @click="sayHello('list-el-1-01')"> two-one
        <ul>
          <li @click="sayHello('list-el-2-01')"> three-one </li> // I click this
        </ul>
      </li>

      <li @click="sayHello('list-el-1-02')"> two-two
        <ul>
          <li @click="sayHello('list-el-2-02')"> three-two </li>
        </ul>
      </li>
    </ul>

  </li>
</ul>

It makes sense that I am also clicking all the wrapping elements, somehow. My question - is there a way to only have the exact element fire it's click event?

like image 475
martinlasek Avatar asked Nov 27 '17 14:11

martinlasek


1 Answers

You can stop propogation using the .stop event modifier as:

@click.stop="sayHello"

Now the event won't be propagated to the parent

More on the list of event modifiers: https://v2.vuejs.org/v2/guide/events.html#Event-Modifiers

like image 106
samayo Avatar answered Sep 29 '22 03:09

samayo