I have a child component calling Parent using $emit. How can we get a return value back from the $emit ?
I tried to return a value as well as include a variable in the function and neither of them work. Is it the nature of the $emit function that can't return a value? Are there any workarounds ?
Child:
let data = null;
await this.$emit('loadData', this.Id, data);
Parent:
async loadData(Id, data) {
let newData = await this.$store.dispatch('user/loadUserData', Id);
data = { ...newData };
}
You don't need to return from
$emit. Usually you mutate a parent's state on events emitted by its child. Then a child use the parent's state to update itself for example through props. That's howv-modelworks btw:VUE SFC PLAYGROUND
If you want to get a value from a parent, just call its method:
VUE SFC PLAYGROUND
Parent:
Child:
You can also use provide/inject:
VUE SFC PLAYGROUND