A brief discussion on the principle of Vue's two-way event binding v-model

A brief discussion on the principle of Vue's two-way event binding v-model

Unlike js or jQuery, which directly changes and operates the DOM, Vue uses v-model to implement two-way binding of data. It will automatically select the correct method to update the element according to the control type.

v-model is a two-way binding instruction of Vue, which can synchronously update the value of the control input on the page to the relevant bound data attribute, and also update the value of the input control on the page when updating the data binding attribute.

The official documentation explains:

v-model is just a syntax sugar, the real implementation still depends on

  • v-bind: Bind responsive data
  • Trigger input event and pass data (core and focus)

The following code

<input v-model="txt"> 

Essentially

<input :value="txt" @input="txt = $event.target.value">

explain:

When the vue instance is initialized, each property of data will be recursively traversed, and the get and set methods of each property will be monitored through defineProperty, so that once a property is reassigned, the change can be monitored to operate the corresponding page control

Look at the code below:

Object.defineProperty(data,"name",{
        get(){
            return data["name"];
        },
        set(newVal){
            let val = data["name"];
            if (val === newVal) {
                return;
            }
            data["name"]=newVal;
            // Listen for changes in attribute values. If node is the corresponding input node, node.value=newVal;
        }    
    })

Summarize

On the one hand, the modal layer hijacks each property through Object.defineProperty, and once the change is detected, it is updated through the relevant page elements. On the other hand, by compiling the template file, the input event is bound to the v-model of the control, so that the page input can update the relevant data attribute value in real time

Object.defineProperty is used to control some special operations of an object's properties, such as read and write rights and whether it can be enumerated. Here we mainly study its corresponding two description properties get and set. v-model actually rewrites its get and set operations. Get is a function triggered by reading the value of the name attribute, and set is a function triggered by setting the value of the name attribute.

Replenish

v-model modifiers: .lazy, .trim, and .number

lazy: After each input event is triggered, the value of the input box is synchronized with the data, and the lazy modifier is added to convert it to using the change event for synchronization

<input v-model.lazy="msg">

trim: remove spaces from the beginning and end of a string

<input v-model.trim="msg">

number: Convert data into value type

<input v-model.number="msg">

This is the end of this article about the principle of Vue's two-way event binding v-model. For more relevant Vue two-way event binding v-model content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of VUE's data proxy and events
  • Detailed explanation of Vue dynamic components and global event binding summary
  • Usage of Vue filters and timestamp conversion issues
  • Detailed explanation of Vue filter implementation and application scenarios
  • Detailed explanation of Vue's data and event binding and filter filters

<<:  Sharing of the fast recovery solution for Mysql large SQL files

>>:  Linux file and user management practice

Recommend

Special commands in MySql database query

First: Installation of MySQL Download the MySQL s...

Stop using absolute equality operators everywhere in JS

Table of contents Overview 1. Test for null value...

VMware 15.5 version installation Windows_Server_2008_R2 system tutorial diagram

1. Create a new virtual machine from VMware 15.5 ...

A brief discussion on whether MySQL can have a function similar to Oracle's nvl

Use ifnull instead of isnull isnull is used to de...

Detailed discussion of the differences between loops in JavaScript

Table of contents Preface Enumerable properties I...

Three ways to communicate between Docker containers

We all know that Docker containers are isolated f...

Computed properties and listeners details

Table of contents 1. Calculated properties 1.1 Ba...

MySQL 8.0.18 stable version released! Hash Join is here as expected

MySQL 8.0.18 stable version (GA) was officially r...

Detailed explanation of FTP environment configuration solution (vsftpd)

1. Install vsftpd component Installation command:...

Summary of solutions for MySQL not supporting group by

I downloaded and installed the latest version of ...

How to solve the mysql ERROR 1045 (28000)-- Access denied for user problem

Problem description (the following discussion is ...

Example code for using @media in CSS3 to achieve web page adaptation

Nowadays, the screen resolution of computer monit...

MySQL cross-database transaction XA operation example

This article uses an example to describe the MySQ...

How to use JavaScript to get the most repeated characters in a string

Table of contents topic analyze Objects of use So...

VMware Workstation installation Linux (Ubuntu) system

For those who don't know how to install the s...