1

The following piece of code is taken from the Vue documentation:

<ChildComponent v-model="pageTitle" />

<!-- would be shorthand for: -->

<ChildComponent :value="pageTitle" @input="pageTitle = $event" />

There is an input event on a component. I understand what an input event does on an input. But what does the input event do on a component?

0

2 Answers 2

1

The @ is short for v-on directive which is used to listen to DOM events emitted/triggered from a specific element. Now most of the native elements will interact with the outside world by emitting their own corresponding events by default. For instance, div element triggers click event, input element triggers input, change, focus and other helpful events.

Unlike native elements, there is absolutely no events triggered BY DEFAULT in a custom component. Therefore, you can only listen to events that are emitted from within the component. Those are custom events, so you can rest assure that none of these event setups below will work unless inside each component emits their own click, input, focus event respectively:

<ComponentA @click="onClickComponentA" />
<ComponentB @input="onInputComponentB" />
<ComponentC @focus="onFocusComponentC" />

In your case, ChildComponent is clearly not a native element so inside this component, it must somewhere emit input event.

Sign up to request clarification or add additional context in comments.

Comments

1

It's all about Custom events, just read Docs

Example:

Vue.component('custom-input', {
  props: ['value'],
  template: `
    <input
      v-bind:value="value"
      v-on:input="$emit('input', $event.target.value)"
    >
  
})

then you can use custom-input like this:

 <custom-input :value="pageTitle" @input="pageTitle = $event" />

where $event is a native event of the input

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.