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. 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: result: Creating a real DOM comes with creating many attributes 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. The relationship between template and virtual DOMThere 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 ( If it is in the default configuration of 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.
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 Vue generates a vnode tree through the following logic: Note: The virtual node tree must be single-rooted injectionVue will inject the following configuration into the Vue instance:
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. MountPlacing 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
SummarizeThis 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:
|
<<: Pure CSS code to achieve flow and dynamic line effects
>>: RGBA alpha transparency conversion calculation table
Table of contents Preface 1. First completely uni...
The 2008.5.12 Wenchuan earthquake in Sichuan took...
This article will introduce some commonly used ar...
The most common, most commonly used and most gener...
Back in the Kernel 2.6 era, a new security system...
Table of contents 1. Introduction 2. First, let...
Comprehensive Documentation github address https:...
1. Introduction Since pictures take up a lot of s...
1 Download MySQL8 from the official website and i...
1. Download the alpine image [root@docker43 ~]# d...
JavaScript clicks to change the shape of the pict...
How to indicate the parent directory ../ represent...
1. Open port 2375 Edit docker.service vim /lib/sy...
Table of contents Preface Demand Analysis Mysql u...
The table creation command requires: The name of...