Skip to content

Instantly share code, notes, and snippets.

@allysonsilva
Created December 2, 2017 03:53
Show Gist options
  • Save allysonsilva/8a9c9bbdc17c6600edb5c307b9947fe8 to your computer and use it in GitHub Desktop.
Save allysonsilva/8a9c9bbdc17c6600edb5c307b9947fe8 to your computer and use it in GitHub Desktop.
Vue.js ⚡️ Eventos - Passando Argumentos - [vm.$emit]
Vue.component('food', {
template: '#food',
props: ['name'],
data: function() {
return {
votes: 0
}
},
methods: {
vote: function(event) {
this.votes++
this.$emit('voted', event.srcElement.textContent)
/*
A função $emit tem como primeiro argumento o nome do evento,
e os demais são parâmetros adicionais à função de callback.
*/
}
},
});
new Vue({
el: '#app',
data: {
votes: 0,
log: []
},
methods: {
countVote: function(food) {
this.votes++
this.log.push(food + ' received a vote.')
}
}
});
<template id="food">
<div>
<h2> {{ votes }} </h2>
<button @click="vote">{{ name }}</button>
</div>
</template>
<div id="app">
<h1> {{ votes }} </h1>
<div>
<food @voted="countVote" name="Cheeseburger"></food>
<food @voted="countVote" name="Double Bacon Burger"></food>
<food @voted="countVote" name="Rodeo Burger"></food>
</div>
<div>
<h1>Log:</h1>
<ul>
<li v-for="vote in log"> {{ vote }} </li>
</ul>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment