Skip to content

Instantly share code, notes, and snippets.

@ErikCH
Created October 10, 2018 04:44
Show Gist options
  • Select an option

  • Save ErikCH/c1d3f84b99d56627bc1eabed501dbd23 to your computer and use it in GitHub Desktop.

Select an option

Save ErikCH/c1d3f84b99d56627bc1eabed501dbd23 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://unpkg.com/vue"></script>
<title>Event this.$listener example</title>
<style>
#hello {
color: blue
}
</style>
</head>
<body>
<div id="el">
<br> Clicked {{clicks}}<br/>
<br> Mouse moved {{mouse}}<br/>
<super-button class="myClass" :id="id" blah="blah" @mouseover="countMouse()" @click="countClicks()"></super-button>
</div>
<template id="super-button-template" >
<button v-bind="attrs" v-on="listeners">Super Button</button>
</template>
<script>
Vue.component('super-button', {
inheritAttrs: false,
computed: {
listeners(){
const {click, ...listeners} = this.$listeners;
console.log(this.$attrs)
return listeners;
},
attrs(){
const {id, ...attrs} = this.$attrs;
return attrs;
}
},
template: '#super-button-template'
});
new Vue({
el: '#el',
data() {
return {
clicks: 0,
mouse: 0,
id: 'hello'
}
},
methods: {
countClicks() {
this.clicks++;
},
countMouse() {
this.mouse++;
}
},
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment