-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using custom components triggers the click event twice #813
Comments
Using custom components triggers the click event twiceVersion3.0.0-alpha.8 Reproduction linknone,not support Steps to reproduceCreate a component named <template>
<button @click="onClick"><slot/></button>
</template>
<script>
export default {
name: 'MButton',
setup (props, { emit }) {
return {
onClick () {
emit('click')
}
}
}
}
</script> Use it in the page. <template>
<div>
<img src="./logo.png">
<h1>Hello Vue 3!</h1>
<m-button @click="inc">Clicked {{ count }} times.</m-button>
</div>
</template>
<script>
import { ref } from 'vue'
import MButton from './MButton'
export default {
components: { MButton },
setup (props, { emit }) {
const count = ref(0)
const inc = () => {
count.value
}
return {
count,
inc
}
}
}
</script>
<style scoped>
img {
width: 200px;
}
h1 {
font-family: Arial, Helvetica, sans-serif;
}
</style>
Click the button. What is expected?
What is actually happening?
|
The click event trigger twice because the event is bubbling.
|
@underfin Is this a new feature?It's diffrence from vue2. |
And version 3.0.0-alpha.7 is fine. |
I tryed to use the <template>
<button @click.stop="onClick"><slot/></button>
</template>
<script>
export default {
name: 'MButton',
setup (props, { emit }) {
return {
onClick () {
emit('click')
}
}
}
}
</script> |
@LastHeaven |
@hareku |
In v3 all |
Yes but this breaks both v-text and class and probably many others too
But the child triggers click.prevent |
That is quite unexpected behavior. When I define a custom event with the same name as a native event (but with a different payload) I would expect it to override the native one. Having both is from my opinion never what is wanted. |
This issue should be took a look again. |
I'm changing my statement on this issue. There is no problem regarding the behavior. If you want to omit specific events you can use |
Worked for me to declare the event with export default {
name: "SomeComponent",
emits: ["click"],
...
} |
In Vue 3, click events fall through meaning each event is fired twice when adding @click prop to the component. Adding `emits: ['click']` tells the component to expect an external click event, therefore preventing the double event. See [here](vuejs/core#813) for more details
I am having the same issue for Vue2.
on my functional component.
I have changed it to functional component. but im still having doublick on the click event.
|
Version
3.0.0-alpha.8
Reproduction link
none,不支持alpha版本的vue
Steps to reproduce
创建一个组件MButton
页面中使用
点击按钮
What is expected?
count从0变成1
What is actually happening?
count从0变成了2
The text was updated successfully, but these errors were encountered: