question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v-on to bind dom event in a custom component

See original GitHub issue

In 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

18reactions
yyx990803commented, May 24, 2016

Hmm, maybe add a modifier:

<foo @click.native="hello">
8reactions
Jinjiangcommented, May 24, 2016

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 preserve v-on in custom component just listen to native dom events and we can have another syntax to listen custom event declaratively instead?

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found