Javascript Virtual DOM Detailed Explanation

Javascript Virtual DOM Detailed Explanation

What is virtual dom?

Virtual DOM is essentially a common JS object (the content of this object is printed in mounted._vnode), which is used to describe the interface structure of the view.

In Vue, each component has a render function, and each render function returns a virtual DOM tree, which means that each component corresponds to a virtual DOM tree.

insert image description here

vnode is a common JS object used to describe what should be on the interface, such as:

var vnode = {
  tag: "h1",
  children: [
    { tag: undefined, text: "The first vue application: Hello World"}
  ]
}

The above object describes:

There is a node with the tag name h1, which has a child node, which is a text with the content "First Vue application: Hello World"

Why do we need virtual dom?

In Vue, rendering the view calls the render function. This rendering occurs not only when the component is created, but also when the data that the view depends on is updated. If the real DOM is used directly during rendering, the creation, update, and insertion of the real DOM will cause a lot of performance loss, which will greatly reduce the rendering efficiency.

Therefore, when rendering, Vue uses virtual DOM instead of real DOM, mainly to solve the problem of rendering efficiency.

Compare the efficiency of creating js objects and real dom objects:

insert image description here

result:

insert image description here

Creating a real DOM comes with creating many attributes

insert image description here

How is the virtual DOM converted to the real DOM?

When a component instance is rendered for the first time, it first generates a virtual DOM tree, then creates a real DOM based on the virtual DOM tree, and mounts the real DOM to the appropriate location in the page. At this time, each virtual DOM corresponds to a real DOM. If the page is refreshed only once and there will be no subsequent data updates, using a virtual DOM is less efficient than directly displaying the real DOM.

If a component is affected by responsive data changes and needs to be re-rendered, it will still call the render function again, create a new virtual DOM tree, compare the new tree with the old tree, find the difference through comparison, and then update only the virtual DOM nodes of the different parts. Finally, these updated virtual nodes will modify their corresponding real DOM

In this way, the actual DOM is minimally modified.

insert image description here

The relationship between template and virtual DOM

There is a compile module in the Vue framework, which is mainly responsible for converting the template into a render function, and the render function will get the virtual DOM after calling it.

The compilation process is divided into two steps:

1. Convert the template string into AST (Abstract Syntax Tree: Use js tree structure to describe our original code; online tool: https://astexplorer.net/)

2. Convert AST to render function

The vue template is not a real DOM, it will be compiled into a virtual DOM

<div id="app">
  <h1>The first Vue application: {{title}}</h1>
  <p>Author: {{author}}</p>
</div>

The above template will be compiled into a virtual DOM similar to the following structure

{
  tag: "div",
  children: [
    { tag: "h1", children: [ { text: "The first Vue application: Hello World" } ] },
    { tag: "p", children: [ { text: "Author: Yuan" } ] }
  ]
}

If you use the traditional import method ( script src="...vue.js "), the compilation time occurs when the component is first loaded, which is called runtime compilation.

If it is in the default configuration of vue-cli , compilation occurs npm run build build). After packaging, there is no template but only render function, which is called template precompilation.

Compilation is an extremely performance-intensive operation. Precompilation can effectively improve runtime performance. Moreover, since compilation is no longer required during runtime, vue-cli will exclude the compile module in vue when packaging to reduce the packaging size.

Whether the compile module needs to be included when packaging is controlled by runtimeCompiler: true in vue.config.js. The default value is false, which means it is not included. It is not recommended to change this configuration

The existence of templates is just to make it easier for developers to write interface codes.

When Vue finally runs, what is needed is the render function, not the template. Therefore, the various syntaxes in the template do not exist in the virtual DOM, and they will all become the configuration of the virtual DOM.

In vue-cli, if both template and render exist, due to the packaging process, the template pre-compilation will generate render to overwrite the original render function.

In Vue, if both template and render exist at the same time, render must prevail.

The virtual DOM tree will eventually be generated into a real DOM tree

insert image description here

Vue generates a vnode tree through the following logic:

insert image description here

Note: The virtual node tree must be single-rooted

injection

insert image description here

Vue will inject the following configuration into the Vue instance:

  • data : data related to the interface
  • computed : data calculated from existing data, which will be explained in detail in the future
  • methods : methods

Members of the Vue instance can be used in the template

To prevent name conflicts. Because the data in data will be proxied to vue, if the data name we write conflicts with the attributes in vue, then the attributes inside vue will be overwritten, so vue will add or before the name of its own attribute member. If it is added with or _, if it is added with or , if it is added with , it means that we can use it. If it is added with _, it is a method or attribute used by vue itself, and we don't need to call it.

Mount

Placing the generated real DOM tree at a certain element position is called mounting

Mounting method:

1. Configure through el:"css selector"

2. Configure through vue instance.$mount("css selector")

Complete Process

  • An instance is created: new Vue()
  • Only after the injection is completed will there be responsiveness and data changes can be monitored
  • Compile and generate a virtual DOM tree: first find the render function, if there is no render function, find a template to generate it, and finally run render to generate a virtual DOM tree
  • Mounting completed: The page displays

insert image description here

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Vue virtual Dom to real Dom conversion
  • Summary of the understanding of virtual DOM in Vue
  • Detailed explanation of virtual DOM in Vue source code analysis
  • Vue Virtual DOM Quick Start
  • Detailed explanation of virtual DOM and diff algorithm in react
  • The principle of Vue virtual DOM
  • Vue uses virtual DOM to render view

<<:  Pure CSS code to achieve flow and dynamic line effects

>>:  RGBA alpha transparency conversion calculation table

Recommend

MySQL 8.0 upgrade experience

Table of contents Preface 1. First completely uni...

Detailed explanation of the 4 codes that turn the website black, white and gray

The 2008.5.12 Wenchuan earthquake in Sichuan took...

React and Redux array processing explanation

This article will introduce some commonly used ar...

Summary of several submission methods of HTML forms

The most common, most commonly used and most gener...

SELinux Getting Started

Back in the Kernel 2.6 era, a new security system...

Analyze the duration of TIME_WAIT from the Linux source code

Table of contents 1. Introduction 2. First, let&#...

Detailed steps for installing rockerChat in docker and setting up a chat room

Comprehensive Documentation github address https:...

Use CSS to implement special logos or graphics

1. Introduction Since pictures take up a lot of s...

Tutorial on installing MySQL8 compressed package version on Win10

1 Download MySQL8 from the official website and i...

Implementation of tomcat image created with dockerfile based on alpine

1. Download the alpine image [root@docker43 ~]# d...

JavaScript implements click to change the image shape (transform application)

JavaScript clicks to change the shape of the pict...

How to write the parent and child directories of HTML relative paths

How to indicate the parent directory ../ represent...

How to use Docker plugin to remotely deploy projects to cloud servers in IDEA

1. Open port 2375 Edit docker.service vim /lib/sy...

Detailed explanation of MySQL cumulative calculation implementation method

Table of contents Preface Demand Analysis Mysql u...

Detailed example of creating and deleting tables in MySQL

The table creation command requires: The name of...