Created
October 10, 2018 04:44
-
-
Save ErikCH/c1d3f84b99d56627bc1eabed501dbd23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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