Home >Web Front-end >JS Tutorial >What is vue virtual DOM? Usage of vue's virtual DOM
Vue’s virtual DOM uses js to simulate the DOM structure. The content of this article is to introduce to friends what vue virtual DOM is? As well as the usage of vue's virtual DOM, let's take a look at the specific content in the article.
1. Why virtual DOM is needed
We wrote a simple Vue-like framework from scratch, in which template parsing and rendering are completed through the Compile function. , document fragmentation is used instead of directly operating the DOM elements in the page. After the data is changed, the real DOM is inserted into the page through the appendChild function.
Although document fragments are used, the real DOM is still operated.
We know that operating DOM is expensive, so vue2.0 uses virtual DOM to replace the operation of real DOM, and finally uses some mechanism to complete the update of real DOM and render the view.
The so-called virtual DOM actually uses JS to simulate the DOM structure, and puts the DOM change operations on the JS layer to minimize the operations on the DOM (I personally think it is mainly because Operating JS is countless times faster than operating DOM, and JS is highly efficient). Then compare the changes in the virtual DOM twice before and after, and only the changed parts will be re-rendered, while the unchanged parts will not be re-rendered.
For example, we have the following DOM structure.
<ul id="list"> <li class="item1">Item 1</li> <li class="item2">Item 2</li></ul>
We can completely use JS objects to simulate the above DOM structure. After simulation, it will become the following structure.
var vdom = { tag: 'ul', attr: { id: 'list', }, children: [ { tag: 'li', attrs: { className: 'item', children: ['Item 1'] }, }, { tag: 'li', attrs: { className: 'item', children: ['Item 2'] } } ] }
It must be noted that the DOM structure simulated by JS does not simulate the properties and methods on all DOM nodes (because the properties of the DOM node itself are very (this is also a point where DOM operations consume performance), but only simulates a part of the properties and methods related to data operations.
2. Usage of vue virtual DOM
Vue introduced vdom in version 2.0. Its vdom is based on modifications made by the snabbdom library. snabbdom is an open source vdom library.
The main function of snabbdom is to convert the incoming JS simulated DOM structure into a virtual DOM node.
First convert the JS simulated DOM structure into a virtual DOM through the h function, and then convert the virtual DOM into a real DOM through the patch function and render it into the page.
In order to ensure the minimum rendering of the page, snabbdom introduces the Diff algorithm, which uses the Diff algorithm to find out the difference between the two virtual DOMs before and after, and only updates the changed DOM nodes without re-rendering them as changed ones. DOM node.
Here I am not going to analyze the source code of snabbdom to explain how snabbdom does this (mainly because it is not at that level at this stage, haha. Moreover, many students have already done similar analysis , relevant links are attached at the end of the article).
I will look at how the virtual DOM in Vue completes view rendering from the perspective of using snabbdom.
Let’s first take a look at the functions of the two core APIs in snabbdom.
h() function: Convert the incoming JS simulated DOM structure template into vnode. (vnode is a pure JS object)
patch() function: Render virtual DOM nodes into the page.
We provide an example to see the actual function of snabbdom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><body> <p id="container"></p> <button id="btn-change">change</button> <!-- 引入snabbdom库,先不必纠结为什么这样引入,以及每个文件的作用。本篇文章只是介绍一下虚拟DOM的工作方式,并不涉及原理解析 主要是因为博主目前功力尚浅,有兴趣的小伙伴可以另行研究 --> <script src="https://cdn.bootcss.com/snabbdom/0.7.1/snabbdom.js"></script> <script src="https://cdn.bootcss.com/snabbdom/0.7.1/snabbdom-class.js"></script> <script src="https://cdn.bootcss.com/snabbdom/0.7.1/snabbdom-props.js"></script> <script src="https://cdn.bootcss.com/snabbdom/0.7.1/snabbdom-style.js"></script> <script src="https://cdn.bootcss.com/snabbdom/0.7.1/snabbdom-eventlisteners.js"></script> <script src="https://cdn.bootcss.com/snabbdom/0.7.1/h.js"></script> <script> //定义patch函数 var patch = snabbdom.init([ snabbdom_class, snabbdom_props, snabbdom_style, snabbdom_eventlisteners ]) //定义h函数 var h = snabbdom.h; //生成一个vnode var vnode = h('ul#list',{},[ h('li.item',{},['Item 1']), h('li.item',{},['Item 2']), ]) //console.log(vnode); //获取container var container = document.getElementById('container'); patch(container,vnode);//初次渲染 var btn = document.getElementById('btn-change'); btn.onclick = function() { var newVnode = h('ul#list',{},[ h('li.item',{},['Item 1']), h('li.item',{},['Item B']), h('li.item',{},['Item 3']), ]) patch(vnode,newVnode);//再次渲染 vnode = newVnode;//将修改后的newVnode赋值给vnode } </script> </body> </html>
Idea analysis:
We first create a virtual DOM node through the h function , render the virtual DOM to the page through the patch function.
When the btn button is clicked, the data of the ul#list list is updated, the value of the second li element is changed and a new li element is added, and the value of the first li element is changed. No change. We again render the updated data to the page through the patch function. You can see that only the second and third li are updated, and the first li is not re-rendered because it has not changed.
The core of template parsing and rendering in vue is: first parse the template through functions similar to snabbdom's h() and patch() into vnode. If it is the first rendering, the vnode is rendered to the page through patch(container,vnode). If it is the second rendering, through patch(vnode,newVnode), first compare the difference between the original vnode and newVnode through the Diff algorithm to Re-render the page with minimal effort.
Recommended related articles:
The object of diff is the virtual dom
The above is the detailed content of What is vue virtual DOM? Usage of vue's virtual DOM. For more information, please follow other related articles on the PHP Chinese website!