I have child component
and want to pass some data to it's parent.
My child component looks like:
// <button @click="sendClick($event)">Send</button>
// ...
data: function (){
return {
mycode: ""
}
},
methods: {
sendClick(e)
{
bus.$emit('change', this.mycode);
}
}
My parent component looks:
var app = new Vue({
el: '#app',
data: {
currentView: 'past-form',
mycode: ''
},
methods:
{
changeView()
{
this.currentView = 'past-form'
console.log(this.mycode);
},
},
created()
{
bus.$on('change', function(mycode){
this.mycode = mycode;
});
}
})
I haven't found a better place for placing
bus.$on
(bus
is declared globally) than increated()
, but the docs state thatcreated()
is for stuff that should be initialized after the page is loaded. Thecreated()
block works; I checked it by placing in itconsole.log(this.mycode)
, but should I move emit handler somewhere else?It's look like my code does not execute
mycode: ''
, becauseconsole.log(this.mycode);
does not print anything.