In Vue 3,v-model
is a syntax sugar used to implement two-way data binding, andv-model:value
yesv-model
The explicit form allows you to specify the bound property name. Their main differences are flexibility and default behavior. The following is a detailed comparison:
1. Default behavior
-
v-model
:- By default,
v-model
Will bind to child componentsmodelValue
Properties andupdate:modelValue
event. - This is the default behavior of Vue 3 and works for most scenarios, especially simple form input bindings.
- By default,
Example:
<template> <ChildComponent v-model="parentValue" /> </template> <script> import ChildComponent from './'; export default { components: { ChildComponent }, data() { return { parentValue: 'Hello' }; } }; </script>
In the child component:
<template> <input :value="modelValue" @input="$emit('update:modelValue', $)" /> </template> <script> export default { props: ['modelValue'], emits: ['update:modelValue'] }; </script>
-
v-model:value
:-
v-model:value
yesv-model
The explicit form allows you to specify the binding attribute and event name explicitly. - This method is more flexible and suitable for scenarios where custom binding properties are required.
-
Example:
<template> <ChildComponent v-model:value="parentValue" /> </template>
In the child component:
<template> <input :value="customValue" @input="$emit('update:value', $)" /> </template> <script> export default { props: ['value'], emits: ['update:value'] }; </script>
2. Flexibility
-
v-model
:- Default binding to
modelValue
andupdate:modelValue
, suitable for most standard scenarios. - If you need to bind multiple attributes, you need to use
v-model:[arg]
Form (e.g.v-model:title
)。
- Default binding to
-
v-model:value
:- Explicitly specify bound properties and events, suitable for scenarios where custom attribute names are required.
- For example, if your child component uses a custom one
value
Properties instead of defaultmodelValue
, you need to usev-model:value
。
3. Multi-model binding
In Vue 3,v-model
Multi-model binding is supported, but it needs to be implemented in an explicit form.
-
v-model
:- Default binding to
modelValue
, but can be extended to multi-model binding by parameters.
- Default binding to
<ChildComponent v-model:title="title" v-model:content="content" />
-
v-model:value
:- Explicit binding, expressing bound properties and events more clearly.
<ChildComponent v-model:value="parentValue" />
4. Usage scenarios
-
v-model
:- Suitable for most standard scenarios, especially form input binding.
- Simple and easy to use, complies with Vue's default behavior.
-
v-model:value
:- Suitable for scenarios where custom binding properties are required.
- Provides greater flexibility, especially when dealing with complex subcomponents.
Summarize
- If your child component uses the default
modelValue
andupdate:modelValue
,usev-model
Just do it. - If you need to customize the binding property name (such as
value
or other custom properties), usev-model:value
orv-model:[arg]
。 - For multi-model binding, parameterized
v-model:[arg]
form to keep the code clear and consistent.
In Vue 3,v-model:value
Provides a more flexible binding method, but the default onev-model
Still the most commonly used form.
This is the end of this article about the difference between v-model:value and v-model in vue3 components. For more information about the differences between vue3 v-model:value and v-model, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!