I am a beginner and I tried vue emit event. But the event do not listen from the parent. Please Help Me!!
In App.vue
<app-header v-bind:somethings='name' @custom-event-name="setName"></app-header>
setName(childName){
this.name= childName;
}
In Body.vue
<button @click="changeName"> click me to change name </button>
changeName: function(){
this.$emit('custom-event-name', 'Some Value'); }
From what I'm seeing in your
scriptsection, you're not importing theBodycomponent, which is where you are trying to emit an event.Right now you have this:
There isn't an
importstatement for theBodycomponent, so your parent component doesn't know whatBodyis. To fix this you just need to add an import like you've done forHeader. It might look like this:import Body from './components/Body.vue';Now that the
Bodycomponent is being used, you need to include it in yourtemplate. You'll do the same thing you did forapp-header, and include a tag like this<app-body></app-body>. Finally, you need to add the event listener so the parent knows when to runsetName. This gets added to theapp-bodytag, and will end up looking like this:<app-body @custom-event-name="setName"></app-body>