2

My html

<a href="/someplace">      
  <div>
    <vuecomp></vuecomp>
    <span>Click row for more info</span>
  </div>
</a>

Then Vue component as such...

<template>
  <div @click.stop="doAction">
  </div>
</template>

When doAction is called it ALSO triggers the <a href=""> from it's parent div. How do I stop it from doing this? I don't want the parent div to become part of the component, it's just a table view. I've tried @click.stop and passing doAction(event) with event.stopPropagation();

Any other ideas, seems like a simple thing to have a button on a click row using vuejs?

2
  • why do you wrap and div and component inside an anchor tag?? Commented Apr 20, 2020 at 11:59
  • Its a table, with more information, a row, that is clickable. Commented Apr 20, 2020 at 12:00

3 Answers 3

2

Add an empty handler to the click event on the anchor with prevent modifier :

 <a href="/someplace" @click.prevent="">

let app = new Vue({
  el: "#app",

  data() {
    return {


    };
  },
  methods: {
    doAction() {

      console.log("do action")
    }
  }

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <a href="/someplace" @click.prevent="">
    <div>
      <div @click="doAction">
      do action
      </div>
      <span>Click row for more info</span>
    </div>
  </a>

</div>

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

Comments

1

You want to disable the default behaviour. So you need .prevent

<template>
  <div @click.prevent="doAction">
  </div>
</template>

Comments

0

What you want to do is turn the parent <a> tag into a plain <div> and attach an event to it, using the .self modifier. That way the click will be handled only when you've clicked onto the parent div itself, and not when you've clicked on any of the child elements inside of it.

<div href="/someplace" @click.self="yourEventToOpenTheRow">      
  <div>
    <vuecomp></vuecomp>
    <span>Click row for more info</span>
  </div>
</div>

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.