Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-emit event with multiple parameters [duplicate]

Tags:

events

vue.js

I am using a component that $emits an event with multiple values. Oftentimes, I just want to re-emit that same event up to higher parent component. When I try @input="$emit('input', $event)", it only re-emits the first parameter.

How can I emit both?

like image 962
Erich Avatar asked Jan 24 '26 15:01

Erich


1 Answers

Option 1: Use a handler method:

<template>
  <component @input="emitInput" />
</template>

<script>
export default {
  methods: {
    emitInput(param1, param2) {
      this.$emit('input', param1, param2);
    },
  },
}
</script>

Option 2: Use an inline function:

<template>
  <component @input="(param1, param2) => $emit('input', param1, param2)" />
</template>

If using a render function, it will look like this:

render(createElement) {
  return createElement(MyComponent, {
    on: {
      input: (param1, param2) => this.$emit('input', param1, param2),
    },
});

Note: This method was inspired by Jacob Goh from his answer and subsequent comment to a similar question that I felt deserved its own question and answer.

like image 92
Erich Avatar answered Jan 26 '26 17:01

Erich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!