Home > OS >  How do I emit multiple arguments via setup()?
How do I emit multiple arguments via setup()?

Time:07-27

I have the following function being passed as an emit to the component:

setTray(tray, pk) {
  alert(tray)
  alert(pk)
},

Calling inside a component, I am able to reach the function, but not the arguments:

setup(props, ctx) {
  ctx.emit('setTray', 'profile-task', pk)
  ctx.emit('setTray', {tray: 'profile-task', pk: pk})
}

Both approaches result in the arguments being undefined when setTray() is executed. What is the correct syntax in this situation?

CodePudding user response:

The emit function accepts one or two arguments, the event name and the payload which in your case should be defined as object :

ctx.emit('setTray', {tray: 'profile-task', pk: pk})

in parent :

setTray({tray, pk}) {//destruct the payload
  alert(tray)
  alert(pk)
},

or in old way :

setTray(payload) {
  alert(payload.tray)
  alert(payload.pk)
},
  • Related