The Wayback Machine - https://web.archive.org/web/20240719155459/https://www.geeksforgeeks.org/vue-js-dom-tree/
Open In App

Vue.js DOM tree

Last Updated : 04 Aug, 2022
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Vue.js is a javascript framework that is used in building static as well as dynamic webpages and User Interfaces. It offers many lucrative features for developers to use. For example, virtual DOM, component architecture, directives, templates, event binding, and many more.

Document Object Model (DOM) is a tree-like representation of a webpage that treats every node as a javascript object, that depicts the part of the document. The DOM indicates the document as a logical tree of the node. It gives us various methods to alter the structure of the tree, create, update, or remove the existing nodes, or even add custom events or event listeners to the HTML elements.

Example of DOM:

<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
        <h2>Welcome To GFG</h2>
        <p>
            Default code has been 
            loaded into the Editor.
        </p>
    </body>
</html>

Tree representation for the above code:

 

Virtual DOM is a lightweight representation of the DOM of a webpage with the help of javascript objects. Every HTML element can be represented as its virtual DOM counterpart. For example:

HTML node:

<h2>Welcome To GFG</h2>

Virtual DOM node:

{
"tag": "h2",
   "children": {
    "text": "Welcome To GFG"
   }
}

Some javascript libraries and frameworks, for instance, vue or react, use the concept of Virtual DOM to make any changes to the real DOM. This is because when the DOM is large and has many nodes present, the operation to change or update the DOM tree becomes very expensive. And so, in order to avoid this, VDOM came into the picture, as it helps us to only update that part of the real DOM which underwent any changes, keeping the rest of the real DOM intact.

How does Virtual DOM Works?

Vue.js uses a reconciliation technique to update the real DOM. With the help of various diff algorithms, vue.js determines whether virtual DOM differs from the real DOM, and then updates only that part of the real DOM that is different from the virtual DOM. 

Example 1: This example describes the creation of a Virtual DOM in Vue.js.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>VueJS DOM Tree</title>
    <script src=
    </script>
</head>
  
<body>
    <h2>GeeksforGeeks</h2>
    <p>This is VueJS DOM tree Example</p>
    <div id="app"> </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                text: 'Hello World'
            },
            render(createElement) {
                return createElement('h1', {
                    attrs: {
                        id: 'headers'
                    }
                }, this.text);
            }
        });
    </script>
</body>
</html>


DOM representation:

 

Output:

 

In the script tag, we create a new element, “h1”, with content as “Hello World”. We then insert the element inside the “div” tag with the id “app”. Upon render, the Vue.js code returns the following HTML element:

Vue.js virtual DOM creates the corresponding HTML element on the real DOM:

<div id="app">
    <h1 id="headers">Hello World</h1>
</div>

From the above example, we can see that the VDOM in vue.js extends the vue.js instance and is essentially composed of javascript objects. 

Example 2: This example illustrates rendering the content by clicking the button.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>VueJS DOM Tree</title>
    <script src=
</head>
  
<body>
    <h1 style="color: green">GeeksforGeeks</h1>
    <h3>Vue.js DOM tree</h3>
    <div id="app">
        <button @click="clickHandler">
            Click me
        </button>
        <p v-if="text.length > 0">
            {{text}}
        </p>
  
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                text: ''
            },
            methods: {
                clickHandler() {
                    this.text = 
        "Welcome to the GeeksforGeeks Learning"
                }
            },
        });
    </script>
</body>
</html>


In the above code:

  • Here, on the initial render of the webpage, we will see a button, saying “Click me”, but there will be no text under it. 
  • Now, when the button is clicked, the <p> tag gets displayed under the button.
  • The vue.js VDOM state changes when the button is clicked, meaning the text variable changes from being an empty string “” to “Welcome to the GeeksforGeeks Learning”. 
  • A diff algorithm then compares the VDOM state to the state of the real DOM, and renders only the <p> tag, instead of re-rendering the whole DOM. 

Output:

 



Previous Article
Next Article

Similar Reads

How to Set URL Query Params in Vue with Vue-Router ?
Vue Router is a navigation library designed specifically for Vue.js applications. In this article, we will learn about the different methods of setting the URL query params in Vue with Vue-Router. Below is the list of all possible methods. Table of Content Using router-linkUsing push() methodUsing replace() methodSteps to Setup the Project Environm
6 min read
Vue.js Render Functions Component VNodes creation
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stag
2 min read
Vue.js v-once Directive
The v-once directive is a Vue.js directive that is used to avoid unwanted re-renders of an element. It treats the element as a static content after rendering it once for the first time. This improves performance as it does not have to be rendered again. First, we will create a div element with the id of choice. The v-once directive is then applied
1 min read
Difference between ReactJS and Vue.js
ReactJS: ReactJS is an open-source JavaScript library created by Facebook which is used to deal with the view layer for both Web and Mobile applications. It can be provided on the server-side along with working on the client-side. Features of ReactJS: Scalability: It is reasonable for enormous scale applications because of its adaptable structure a
2 min read
Vue.js | Routing
Routing is one of the many features provided by Vue.js to allow users to switch between pages without refreshing every time a page is loaded. This results in smooth transitions between pages giving a better feel for the user.Setting Up The Application: Firstly, we need to create a project to work on. We can use Vue CLI (Command Line Interface) for
3 min read
Vue.js | v-text directive
The v-text directive is a Vue.js directive used to update a element’s textContent with our data. It is just a nice alternative to Mustache syntax. First, we will create a div element with id as app and let's apply the v-text directive to this element with data as a message. Now we will create this message by initializing a Vue instance the data att
1 min read
Vue.js | v-html directive
The v-html directive is a Vue.js directive used to update a element’s inner HTML with our data. This is what separates it from v-text which means while v-text accepts string and treats it as a string it will accept string and render it into HTML. First, we will create a div element with id as app and let's apply the v-html directive to this element
2 min read
Vue.js | v-show directive
The v-show directive is a Vue.js directive used to toggle the display CSS property of a element with our data via inline styles. If the data is true it will make it visible else it will make it invisible. First, we will create a div element with id as app and let's apply the v-show directive to this element with data as a message. Now we will creat
2 min read
Vue.js | v-on:click directive
The v-on:click directive is a Vue.js directive used to add a click event listener to an element. First, we will create a div element with id as app and let's apply the v-on:click directive to a element. Further, we can execute a function when click even occurs. Syntax: v-on:click="function" Parameters: This directive accepts function which will be
1 min read
Vue.js | v-else directive
The v-else directive is a Vue.js directive used to toggle the display CSS property of a element only when the if condition is not satisfied. First, we will create a div element with id as app and let's apply the v-else directive to a element with data. Now we will create this data by initializing a Vue instance with the data attribute containing th
2 min read
three90RightbarBannerImg