【问题标题】:Vue components Event HandlingVue 组件事件处理
【发布时间】:2019-08-11 15:03:23
【问题描述】:

我有一个组件按钮,我几乎在网站的任何地方都使用该按钮。

它是一个带有一些悬停动画的 SVG 按钮。该按钮工作正常。

<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>

现在我想将此按钮用作表单中的提交按钮。悬停事件正在发生,但似乎 @click 事件没有触发。

可能我错过了一些不好的东西。

<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吗?

标签: vue.js event-handling components nuxt.js


【解决方案1】:

您可以像评论中建议的那样使用@click.native (see here) 或将@click 放入您的AnimButton$emit 某个事件中并在父组件中捕获此事件。

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 2017-07-09
    • 2021-12-14
    • 1970-01-01
    • 2020-09-18
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    相关资源
    最近更新 更多