Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
Is there a way I can nest a button inside of a router link, and when it's clicked to only trigger the button instead of both?

To clarify, I have this router link inside a navbar, which has a button on top of it that changes the route.

Here's code for context:

<router-link v-if="foo" :to="/">
...
  <button @click="redirect"> GO </button>
...

The method for redirection:

redirect() { this.$router.push('/myroute').catch(() => {})

I tried using z index on both elements, but every time I click on the GO button it first goes to the '/myroute' and then goes back to '/'

Is there a way I can work around this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could call event.stopPropagation() to prevent this event being propagated in the bubbling phase. So the event will stop only on the button element.

redirect(e) {
    e.stopPropagation();
    this.$router.push('/myroute').catch(() => {});
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the stop modifier to @click.

<router-link v-if="foo" :to="/">
...
  <button @click.stop="redirect"> GO </button>
...

This is the vue equivalent of stopPropagation.

The z-index is more about the display of the button. Events propagate such that when you click on an inner DOM element, you're also clicking on everything that's at the (x,y) including the containing node. If there's nothing else listening to that event (which is usually the case) then the click does what you want.

Another alternative is to make the button and the router-link siblings, and use an outer <div></div> with some flex styling to place them where you want.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda