Why Vue v-model expand differently in component and native element

88 Views Asked by At

In the docs, it is said that:

<input v-model="searchText" />
// becomes
<input
  :value="searchText"
  @input="searchText = $event.target.value"
/>

but

<CustomInput v-model="searchText" />
// becomes
<CustomInput
  :model-value="searchText"
  @update:model-value="newValue => searchText = newValue"
/>

Note the :model-value/modelValue props. Why doesn't custom input props become :value, just like the native one? Like this:

<CustomInput v-model="searchText" />
// becomes
<CustomInput
  :value="searchText"
  @input="newValue => searchText = newValue"
/>
1

There are 1 best solutions below

0
On
  1. If you use @input on custom components it would be impossible to bind this event to the real <input> used in a component.
  2. Having different event and prop names for native and custom components is a good idea to avoid any possible conflicts.
  3. v-model could handle multiple props: v-model.prop2 - some other prop could be bound too.
  4. @input="newValue => searchText = newValue" - you can't do that since newValue is an event, not a new value.
  5. using v-model on native inputs is a good abstraction to avoid bothering about which exact native event we need for value changes.
  6. If you are a novice I wouldn't question decisions made by a framework's creators being you yet.