I am creating a small client-side Vuejs application and have a bit of trouble with assigning data to a variable. In a login component I am emitting user data with an eventBus, and in an index component I receive the data from the eventbus within the mounted: function (). I have no trouble with receiving the data, but what I would like to do is assign this received data to the currentUser variable. This is what I have in the index component:
mounted: function () {
eventBus.$on('thisEvent', function (userObject) {
console.log('event received!', userObject)
this.currentUser = userObject.user
console.log('The user: ', this.currentUser) // Shows correct new user data
}.bind(this))
console.log('User outside eventbus:', this.currentUser) // Shows empty user
}
And this is how the user looks like in the data:
data: function () {
return {
currentUser: {
firstname: '',
lastname: '',
last_login: ''
}
}
}
I cannot figure out why the currentUser is emptied outside the eventbus. Thanks in advance!
**Edit: ** This is how I have it now in my index.vue
This is the $emit of the eventBus, this is being called when the get request response is successfull. The code is in the login.vue, where an authenticated user object gets filled, and transfers this data to the index.vue.
eventBus.$emit('thisEvent', {
user: this.user
})
console.log(this)(inside theeventBus.on) didnt print anything ?