Add event listener to component using “v-

2019-02-07 23:46发布

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>

3条回答
女痞
2楼-- · 2019-02-08 00:29
<router-link:to="to">
    <span @click="InlineButtonClickHandler">{{name}}</span>
</router-link>

Maybe you can try this.

查看更多
forever°为你锁心
3楼-- · 2019-02-08 00:40

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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-02-08 00:47

a nice solution would be:

<a v-link='{name: "home"}' v-on:click.capture='InlineButtonClickHandler'>Home</a>

查看更多
登录 后发表回答