Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a value from $emit payload with Vue.js

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?

like image 560
user3325126 Avatar asked Oct 23 '18 00:10

user3325126


People also ask

How do you use $emit in VUE JS?

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 .

How do I pass my value to component Vue?

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!


1 Answers

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
   }
like image 64
Boussadjra Brahim Avatar answered Sep 28 '22 06:09

Boussadjra Brahim