The V-Model Directive and Two-Way Data Binding
Explore how to implement two-way data binding using the v-model directive in Vue 3. Understand the migration changes from Vue 2 to Vue 3, including the deprecation of the .sync modifier and model option. Learn to use multiple v-models on single components and update custom components to use the modelValue prop and update:modelValue event for improved reactivity.
We'll cover the following...
Two-way data binding using v-model
Two-way data binding on a form element or component can be achieved using the v-model directive. Below is a simple example of using this directive.
Vue 2
Let’s look at the script for the my-component component:
The v-model directive is over the value prop and input event, syntactically speaking. These can be renamed by using the model option:
The model option
Let’s look at the updated script for the my-component component:
In Vue 2, only one v-model can be specified on a component.
Vue 2 - sync modifier
Two-way data binding for multiple props could be achieved using the .sync modifier:
Now look at the script for the my-component component:
Vue 3
The v-model directive was reworked in the new version of Vue. It can now be used multiple times on the same component. Because of this ...