Tanmay
Tanmay

Reputation: 3159

Receiving input value and some other data together in vue

I have a custom input component that looks like this:

<select @change="$emit('change', $event.target.value)">
...
</select>

As you can see I am emitting a change event with the current input value (hence, $event.target.value).

I use that component like this:

<mycomponent @change="someMethod"></mycomponent>

as you can see, the value is automatically received by someMethod. In my root vue instance I have this someMethod defined:

someMethod(value){
    console.log(value);
}

But what if I wanted to pass some other value to the someMethod method along with the input value? How can I do that?

<mycomponent @change="someMethod('My String Data')"></mycomponent>

How can I receive both the input value and 'My String Data' in someMethod?

Upvotes: 0

Views: 56

Answers (1)

Jakub Kutrzeba
Jakub Kutrzeba

Reputation: 1003

You can access the emitted event’s value in an inline statement handler. You can pass it into a method using the special $event variable:

<mycomponent @change="someMethod($event, 'My String Data')"></mycomponent>

Upvotes: 3

Related Questions