How to implement two-way binding function in vue.js with pure JS

How to implement two-way binding function in vue.js with pure JS

See the screenshot below

This is a normal HTML file, and Vue.js is not introduced. Do you see anything familiar in the code? For example: "v-model", "v-on:click", and the commonly used "double curly braces {{}}" assignment statement.

First, let's talk about the idea of ​​implementing two-way binding:

1. Create a custom Vue js object, such as the wslVue object above. The parameters required in the initialization method are: (1) the DOM object ID to be mounted, (2) the data attribute of the custom Vue object (JSON object), and (3) a simulated mounting method is added later. (The parameters (1) and (2) are useful here. The parameter (3) can be used to assume that the rendering (mounting) method callback can be performed after all the initialization work of the Vue object is completed).

2. Rewrite the set and get methods of the vue attribute data json object. At the same time, you can add all the attributes under data to the vue object, rewrite the set and get methods of the vue-generated attributes, and directly execute the set and get methods of data in the method (the purpose is to directly use the reading and writing of vue object attributes for dom operations).

3. Parse HTML and reorganize the tag nodes and text nodes in HTML (the specific reorganization here refers to converting the Vue instructions and {{}} assignment symbols into normal HTML documents for output). During the parsing process, cache, bind logic, and add listening events for each node that needs to be operated (for example: input tag input).

Let's talk about the main methods of js to implement these functions:

1. Set and get methods of js object properties.

2. document.createDocumentFragment html fragment parsing.

3. Reorganize the HTML code snippets based on relevant regular expressions.

What tool classes need to be created in the end?

1. Vue object.

2. The observer class Watcher saves the node nodes that need to be operated and the callback methods that need to be made when the attributes are changed.

3. Manage the management class Dep of all observers Watcher, and control the callback rendering of Watcher related to data changes.

Implementing Vue two-way binding

Initialize Vue object method

Notes:

1: Add all the properties in data to the vue object and rewrite the set and get methods.

2: Add a method management methods object to the vue object. When parsing the HTML to obtain the v-on:click method, add a click event method body to the tag.

3: Parse the HTML here. When encountering a node that needs to be processed during parsing, create a Watcher object, save the relevant node and instructions in the Watcher object, and add the Watcher object to the observer management class Dep collection.

4: After initialization is completed, mount it and render the complete HTML to the specified DOM element.

Compile class parsing requires mounting the corresponding dom

Get all the node nodes

Parsing specific instructions

Label elements and text content judgment

If it is a tag node here, you need to parse the v-on and v-model instructions inside

v-model

v-on:click

The red line is the method matched by methods in the vue object, which adds a click event to the current node.

If the node is text content, the {{}} directive inside needs to be parsed.

Summary: Many Watcher objects will be created here. The objects save the current vue object, node, data change callback, and are saved in the Dep management class, so that when the data changes, the method callback will be directly executed for rendering.

Specific instruction judgment

Watcher and Dep objects

Finally, put a mind map

Conclusion: This is the end of the basic idea. There is no complicated logic and the expressive ability is limited. I hope this will be helpful to everyone, and I also accept criticism and correction from the masters so that we can make progress together.

The above is the details of how pure JS can implement the two-way binding function under vue.js. For more information about JS to implement vue's two-way binding, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Example code of vue custom component to implement v-model two-way binding data
  • The principle and implementation of two-way binding in Vue2.x
  • Implementation of two-way binding of parent-child component data in front-end framework Vue
  • A brief discussion on the principle of Vue's two-way event binding v-model
  • Using js to implement the two-way binding function of data in Vue2.0
  • Detailed explanation of Vue two-way binding

<<:  How to optimize MySQL performance through MySQL slow query

>>:  A brief analysis of the matching priority of Nginx configuration location

Recommend

Vue3.0 handwriting magnifying glass effect

The effect to be achieved is: fixed zoom in twice...

MySQL 5.7.17 installation and configuration graphic tutorial

Features of MySQL: MySQL is a relational database...

MYSQL Operator Summary

Table of contents 1. Arithmetic operators 2. Comp...

Split and merge tables in HTML (colspan, rowspan)

The code demonstrates horizontal merging: <!DO...

Use of Linux crontab command

1. Command Introduction The contab (cron table) c...

Detailed explanation of vite2.0+vue3 mobile project

1. Technical points involved vite version vue3 ts...

JavaScript to implement random roll call web page

JavaScript writes a random roll call webpage for ...

How to install mysql5.7.24 binary version on Centos 7 and how to solve it

MySQL binary installation method Download mysql h...

js canvas to realize the Gobang game

This article shares the specific code of the canv...

Detailed graphic tutorial on installing centos7 virtual machine in Virtualbox

1. Download centos7 Download address: https://mir...

Example analysis of mysql shared lock and exclusive lock usage

This article uses examples to illustrate the usag...

In-depth explanation of JavaScript this keyword

Table of contents 1. Introduction 2. Understand t...