In Vue3, two-way binding is through
v-model
The implementation of the instruction mainly depends onResponsive systemandEvent mechanism. The responsive system in Vue3 isProxy
Implementation, it replaces the, providing more powerful features and better performance.
The following is the basic principles and code example analysis of Vue3's implementation of two-way binding:
1. The principle of two-way binding
Data responsiveness:
Vue3 UseProxy
Hijacking data objects, and when data changes, view updates can be sensed and notified.
Event listening:v-model
Essentially syntactic sugar, it is equivalent to bindingvalue
andinput
event. Component passprops
Receive data (modelValue
),passemit
Trigger event notify the parent component (update:modelValue
)。
2. Code example: implement two-way binding in ordinary components
Parent component code:
<template> <div> <h1>Vue3 Two-way binding example</h1> <!-- usev-modelTwo-way binding --> <CustomInput v-model="inputValue" /> <p>Values in parent component:{{ inputValue }}</p> </div> </template> <script> import CustomInput from './'; export default { components: { CustomInput }, data() { return { inputValue: '' // Parent component's data }; } }; </script>
Subcomponent code:
<template> <div> <!-- The child component accepts the value passed by the parent component --> <input :value="modelValue" @input="onInput" /> </div> </template> <script> export default { props: { // Receive the value passed by the parent component modelValue: { type: String, required: true } }, methods: { // Trigger event to pass data back to the parent component onInput(event) { this.$emit('update:modelValue', ); } } }; </script>
Execution process:
Parent component usagev-model="inputValue"
Bind data.
Vue3 willv-model="inputValue"
Resolution as:
:modelValue="inputValue" @update:modelValue="value => inputValue = value"
The parent component willinputValue
Value passedmodelValue
Pass to child components.
Subcomponent monitoringinput
Event, when the content of the input box changes,$emit('update:modelValue', newValue)
Notify the parent component to update the data.
Parent component updateinputValue
, trigger responsive updates, and the view is refreshed synchronously.
3. Analysis of core code of Vue3 responsive system
Vue3's responsive system usageProxy
accomplish. Here is a simplified version of the core principle:
// Define responsive objectsfunction reactive(target) { return new Proxy(target, { get(target, key, receiver) { (`Access properties: ${key}`); return (target, key, receiver); }, set(target, key, value, receiver) { (`Set properties: ${key} for ${value}`); const result = (target, key, value, receiver); // Responsive trigger view update triggerUpdate(); return result; } }); } function triggerUpdate() { ('View Update'); } // Exampleconst state = reactive({ name: 'Vue3', count: 0 }); (); // Access properties: name++; // Set properties: count for 1,Trigger view update
4. In-depth analysis of the working principle of v-model
Default binding and event:
In Vue3,v-model
The default binding and event are as follows:
- Bind attributes:
modelValue
- Trigger event:
update:modelValue
Custom binding fields:
You can customizev-model
Bound fields, for example:
<CustomInput v-model:title="titleValue" />
At this time Vue3 will resolve to:
:Title="titleValue" @update:Title="value => titleValue = value"
Subcomponents need to support customizationprops
andemit
:
<script> export default { props: { title: { type: String, required: true } }, methods: { onInput(event) { this.$emit('update:title', ); } } }; </script>
5. Advantages of Vue3 Bidirectional Binding
Performance improvement:
useProxy
After that, there is no need to define getter/setter separately for each property.
Increased flexibility:v-model
Supports multiple binding fields.
Modular and clear:
By explicitprops
andemit
, make data flow more transparent.
This is the article about the basic principles and code example analysis of Vue3’s implementation of two-way binding. For more related content on Vue3, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!