I have a Vue component that has an $emit event. I'd like to have a method that receives the callback (with data from the $emit), but also accepts another parameter. I would think that having a method return a function would accomplish this, but I can't seem to get it working.
<my-component @callback="notifyCallback" />
I can easily receive the data from the $emit like this:
methods: {
notifyCallback(data) {
console.log(data)
}
}
But I would like to use the same callback method for multiple $emit events with different names, while still receiving the data sent from the $emit. My thinking is that having the method return a function would do the trick. Something like this:
<my-component
@callback-a="notifyCallback('callback-a')"
@callback-b="notifyCallback('callback-b')"
/>
methods: {
notifyCallback(callbackKey) {
return function(data) {
console.log(`Callback: ${callbackKey} has data: ${data}`)
}
}
}
But this approach of having the method return a function doesn't seem to work.
Am I thinking about this incorrectly? Is there a way to accomplish having a method return a function?
Or, more directly at my desired goal ... is there a way to have a callback method receive an extra parameter along with the data in the $emit?
Thanks so much for your input!
Note: I realize I could pass the callbackKey with the $emit data, but the approach of having a method return a function seems like it should work, so I'm mostly curious about why it isn't, and what I'm misunderstanding.