8

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?

1 Answer 1

23

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

Sign up to request clarification or add additional context in comments.

3 Comments

Works with vue js 2.96
How to do the opposite? Stop click from going to child.
@MarsAndBack try using .self in the parent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.