0

Here what I am trying is I have dynamic data and displaying it in section and section is clickable and under that, I have 2 buttons called edit, del after clicking those buttons also it should trigger some action.

The problem I am facing is even though I am clicking edit action but the section button click also getting triggered and I tried putting @click.prevent still facing the issue.

what I need is whenever I click on edit or del the section action should not trigger below is my code sandbox URL

https://codesandbox.io/s/vuejs-with-import-json-example-forked-tjn45?fontsize=14&hidenavigation=1&theme=dark

code

<template>
  <div>
    <section
      style="border-style: dotted"
      v-for="(name, index) in names"
      :key="index"
      @click.prevent="methodOne"
    >
      <div>
        <button @click.prevent="edit">Edit</button>
        <button @click="del">Del</button>
      </div>
      <div>
        {{ name }}
      </div>
    </section>
  </div>
</template>

<script>
import states from "../assets/data.json";
export default {
  name: "HelloWorld",
  computed: {
    names() {
      return states.accounts.map((item) => {
        return item.name;
      });
    },
  },
  methods: {
    methodOne() {
      alert("Method One");
    },
    edit() {
      alert("Edit");
    },
    del() {
      alert("DELETE");
    },
  },
};
</script>

1 Answer 1

2

This is event propagation issue, .prevent don't stop the propagation, Use .stop instead of .prevent. Update example here

<div>
  <button @click.stop="edit">Edit</button>
  <button @click.stop="del">Del</button>
</div>
Sign up to request clarification or add additional context in comments.

Comments

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.