向 <router

编程入门 行业动态 更新时间:2024-10-27 18:23:14
本文介绍了向 &lt;router-link> 添加事件监听器使用“v-on:"的组件指令 - VueJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自定义处理程序 InlineButtonClickHandler 添加到 组件的 click 事件,以便我可以发出自定义 appSidebarInlineButtonClick 事件.

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>

推荐答案

您需要添加 .native 修饰符:

You need to add the .native modifier:

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

这将监听 router-link 组件的根元素的原生点击事件.

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

这篇关于向 <router-link> 添加事件监听器使用“v-on:"的组件指令 - VueJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 10:17:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/801400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:lt   router

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!