Skip to content
Advertisement

Setting a ‘default’ v-on event for a component within Vue

I have a set of ‘text-input’ custom components which house some boilerplate markup and an ‘input’ element.

One way of getting the value of the ‘text-input’ in to it’s parent is to $emit an event when the value has changed.

I need to capture and handle the $emit with a v-on for every text-input component:

JavaScript

I feel that this introduces too much repetition in the code, and I was wondering if there were a way to have the v-on listener on the component template itself:

JavaScript

So that there is a ‘default’ v-on listener for this component, every time it is used.

Advertisement

Answer

You can use v-model on custom components.

html

JavaScript

script

JavaScript

here is the example fiddle

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement