简体   繁体   English

Vue 组件事件处理

[英]Vue components Event Handling

I have a component button where I use that button almost everywhere in my site.我有一个组件按钮,我几乎在网站的任何地方都使用该按钮。

Its an SVG button with some animation on hover.它是一个 SVG 按钮,悬停时有一些动画。 The button works fine.该按钮工作正常。

 <template> <a class="button" @mouseover="buttonEnter" @mouseout="buttonLeave"> <svg viewBox="0 0 180 60"> <path ref="btnPath" d="..." /> </svg> <span ref="btnSpan"> <slot>Button slot</slot> </span> </a> </template> <script> import { buttonEnter, buttonleave } from '../assets/animate' export default { name: 'AnimButton', methods: { buttonEnter(event) { buttonEnter(this.$refs.btnPath, this.$refs.btnSpan) }, buttonLeave(event) { const buttonSpan = event.currentTarget.querySelector('span') buttonleave(this.$refs.btnPath, this.$refs.btnSpan) }, }, } </script>

Now I wanna use this button as a submit button in my form.The hover events are happening, but seems the @click event is not triggering.现在我想将此按钮用作表单中的提交按钮。悬停事件正在发生,但似乎@click事件没有触发。

Probably i'm missing something bad.可能我错过了一些不好的东西。

 <form> <input type="email"> <input type="subject"> <input type="message"> <anim-button type="submit" @click="submit"> Submit </anim-button> </form> <script> import AnimButton from '~/components/AnimButton.vue' export default { components: { AnimButton, }, methods: { submit: function() { console.log('submit') }, } </script>

您既可以像评论中建议的那样使用@click.native参见此处),也可以将@click放在您的AnimButton中, $emit一些事件并在父组件中捕获此事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM