v-on to bind dom event in a custom component
See original GitHub issueIn Vue 2.0 if we need to offer some common dom event features, we have to write each of them in custom components itself.
for example
main.vue
:
<template>
<btn @click="go">Click Me!</btn>
</template>
<script>
module.exports = {
components: {
btn: require('btn.vue')
}
methods: {
go: function (e) {
// todo: go to a certain url
}
}
}
</script>
btn.vue
:
<template>
<button @click="click" @mouseenter="mouseenter" @mouseleave="mouseleave" ...><slot></slot></button>
</template>
<script>
module.exports = {
methods: {
click: function (e) {this.$emit('click', e)},
...
}
}
</script>
But I think that’s not necessary.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:7 (5 by maintainers)
Top Results From Across the Web
How should I bind an event to DOM elements created with a ...
Right now, I'm binding events to the parent element of my custom tag's rendered content, then using classes to target the event onto...
Read more >Custom events in JavaScript: A complete guide
In this tutorial, you can learn how to create custom events in JavaScript for your application to enhance the user experience.
Read more >Shadow DOM and events - The Modern JavaScript Tutorial
Events that happen in shadow DOM have the host element as the target, when caught outside of the component. Here's a simple example:....
Read more >This is why we need to bind event handlers in Class ...
We need to bind these methods to the component instance using .bind() in our custom component's constructor. class Foo extends React.
Read more >How To Handle DOM and Window Events with React
In this step, you'll create a validating component using an <input> HTML element and the onChange event handler. This component will accept ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hmm, maybe add a modifier:
Well in another way, I think
v-on
in most html elements mean native dom event so that’s also confused me sometimes (such as why<div @click>
works but<foo @click>
not). How about preservev-on
in custom component just listen to native dom events and we can have another syntax to listen custom event declaratively instead?