Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save allysonsilva/4a7cbf08be8a327b391ead5a09c1286a to your computer and use it in GitHub Desktop.
Save allysonsilva/4a7cbf08be8a327b391ead5a09c1286a to your computer and use it in GitHub Desktop.
Vue.js ⚡️ Eventos - Comunicação entre Pai e Filho
Vue.component('food', {
template: '#food',
props: ['name'],
methods: {
vote: function() {
this.$emit('voted')
}
},
});
new Vue({
el: "#app",
data: {
votes: 0
},
methods: {
countVote: function() {
this.votes++
}
}
});
<template id="food">
<button @click="vote">{{ name }}</button>
</template>
<div id="app">
<div>
<h1> {{ votes }} </h1>
<food @voted="countVote" name="Cheeseburger"></food>
<!--
@voted="countVote" significa que quando o componente filho emitir o evento voted,
o método countVote será executado.
-->
</div>
<pre>
{{$data}}
</pre>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment