iTranslated by AI
Reviewing the Two Types of Two-Way Data Binding in Vue and Fully Understanding defineModel()
I got confused with two-way data binding and the defineModel() added in Vue 3.4, so I'm organizing my thoughts here.
What is Data Binding?
The explanation from Wikipedia is the easiest to understand.
Data binding is a general technique that binds data sources from the provider and consumer together and synchronizes them. In data binding, a change to the data is optionally reflected by the elements that are bound to the data.
Source: Wikipedia
What is Two-Way Data Binding?
In Vue, there are two types of two-way data binding.
1. Two-way data binding between parent and child components
This refers to the automatic synchronization of data changes between a parent component and a child component.
- If data is changed in the parent component, the change is automatically reflected in the child component.
- If data is changed in the child component, the change is automatically reflected in the parent component.
2. Two-way data binding within a component
This refers to the synchronization of data changes between the data inside a component and the View (template).
- If the data is changed, the change is automatically reflected in the View.
- If the data is changed on the View side, the change is automatically reflected in the data.
Next, I will explain how to implement these two types of two-way data binding.
Implementation of Two-Way Data Binding
1. Two-way data binding between parent and child components
Before Vue 3.4, to achieve two-way data binding between a parent component and a child component, we implemented it using:
- v-model
- defineProps
- defineEmits
(I will explain two-way data binding using defineModel() later.)
<script setup>
import { ref } from 'vue'
import child from './components/child.vue'
const text = ref('Hello')
</script>
<template>
<child v-model="text" />
</template>
At this time, remember that the v-model being passed to the component is syntactic sugar for:
<child :modelValue="text" @update:modelValue="val => text = val" />
<script setup>
defineProps({ modelValue: String });
defineEmits(['update:modelValue']);
</script>
<template>
<p>{{ modelValue }}</p>
<input
type="text"
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
/>
</template>
It is not recommended for child components to directly change the parent component's data (props).
Therefore:
- defineProps: defines props and receives data from the parent component.
- defineEmits: defines the method to update the props.
2. Two-way data binding within a single component (SFC)
Next, two-way data binding within a component can be implemented as follows:
<script setup>
import { ref } from 'vue';
const message = ref(''); // Define reactive data
</script>
<template>
<input v-model="message" /> <!-- Bind data and form elements two-way -->
</template>
On the View side, when something is entered into the input, the reactive data changes. Conversely, if the reactive data changes, that change is reflected in the input element.
At this time, remember that the v-model being passed to the input is syntactic sugar for:
<input :value="message" @input="message = $event.target.value" />
What is defineModel()?
Since Vue 3.4, two-way data binding between parent and child components can be written as follows using defineModel():
<script setup>
import { ref } from 'vue'
import child from './components/child.vue'
const text = ref('Hello')
</script>
<template>
<child v-model="text" />
</template>
<script setup>
const text = defineModel()
</script>
<template>
<p>{{ text }}</p>
<input v-model="text" />
</template>
defineModel() returns a ref, which is a reactive value.
This ref can be accessed or assigned via .value just like a regular ref.
However, this ref differs from a normal ref in the following way:
The value of the ref returned by defineModel() is automatically synchronized with the data being passed as v-model in the parent component.
In other words, changing the value in the child component will automatically update the parent's data, achieving (1) two-way binding between parent and child components.
Furthermore, since you can also pass a ref to v-model within the child component, you can also achieve (2) two-way data binding within a single component (SFC) as mentioned earlier.
Discussion