Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add event listener to <router-link> component using "v-on:" directive - VueJS

I'm attempting to add a custom handler InlineButtonClickHandler to a <router-link> component's click event, so that I can emit a custom appSidebarInlineButtonClick event.

But, my code isn't working. What am I doing wrong?

<template>    <router-link :to="to" @click="InlineButtonClickHandler">      {{ name }}    </router-link> </template>  <script type="text/babel"> export default {   props: {     to: { type: Object, required: true },     name: { type: String, required: true }   },   methods: {     InlineButtonClickHandler(event) {       this.$emit('appSidebarInlineButtonClick');     }   } }  </script> 
like image 504
Evgeniy Miroshnichenko Avatar asked Feb 07 '17 14:02

Evgeniy Miroshnichenko


People also ask

How do I add event listeners to Vue?

To add an event listener to a component using the v-on directive with Vue. js, we should add the native modifier. to set the add the native modifier to @click and set it to buttonClickHandler . Then buttonClickHandler will run when we click on the router-link component.

How do I redirect route Vue?

You can use $router. push({ name: "yourroutename"}) or just router. push("yourroutename") now to redirect.


2 Answers

You need to add the .native modifier:

<router-link     :to="to"     @click.native="InlineButtonClickHandler" >     {{name}} </router-link> 

This will listen to the native click event of the root element of the router-link component.

like image 100
thanksd Avatar answered Oct 15 '22 06:10

thanksd


<router-link:to="to">     <span @click="InlineButtonClickHandler">{{name}}</span> </router-link> 

Maybe you can try this.

like image 20
许浩东 Avatar answered Oct 15 '22 04:10

许浩东