I'm emitting an event with various values from my ConversationList (child component) to ConversationModel (parent component).
Conversation List
getConversation(conversation_id, receiver_id, username, avatar){
this.$emit('open-conversation', receiver_id, conversation_id, username, avatar);
}
ConversationModal
Vue devtools output
[2,1,"Jeff","http://i.pravatar.cc/52"]
Template
<converstations-list v-if="showConversation == false" v-on:open-conversation="getConversation($event)"></converstations-list>
Method
getConversation(event){
console.log(event.payload[0] + event.payload[1] + event.payload[2] + event.payload[2])
},
Error
Error in event handler for "open-conversation": "TypeError: Cannot read property '2' of undefined"
I don't get it. The event is firing and has a payload object in the devtools
that I can't access. What am I doing wrong?
Emitting Events with setup() In the Composition API, if we use the setup function, we don't have access to our component with this - meaning we can't call this. $emit() to send our event. Instead, we can access our emit method by using the second argument of our setup function – context .
Using Props To Share Data From Parent To Child # VueJS props are the simplest way to share data between components. Props are custom attributes that we can give to a component. Then, in our template, we can give those attributes values and — BAM — we're passing data from a parent to a child component!
in the event emitting from child component to the parent one, you will receive the parameters like you emit them :
So remove $event
from your getConversation
handler:
<converstations-list v-if="showConversation == false" v-on:open-conversation="getConversation"></converstations-list>
and implement the getConversation
method as follow :
getConversation(receiver_id, conversation_id, username, avatar){
// do whatever you want with that parameters
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With