Home  >  Article  >  Web Front-end  >  What is vue virtual DOM? Usage of vue's virtual DOM

What is vue virtual DOM? Usage of vue's virtual DOM

不言
不言Original
2018-08-09 11:00:3412607browse

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.

  • Item 1
  • Item 2

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.




    
    
    
    Document
    

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn