Home  >  Article  >  Web Front-end  >  The difference between mounted and created in Vue (detailed explanation with pictures and text)

The difference between mounted and created in Vue (detailed explanation with pictures and text)

烟雨青岚
烟雨青岚forward
2020-07-01 11:41:467895browse

The difference between mounted and created in Vue (detailed explanation with pictures and text)

1. What is life cycle?

In popular language, it is a series of processes that an instance or component in Vue goes through from creation to destruction. Although it is not rigorous, it is basically understandable.

Through a series of practices, now I have sorted out all the problems encountered, and today I will record the difference between created and mounted:

2. What is the difference between created and mounted?

The official diagram is as follows:


##We look at two nodes from the diagram:

created: Called before the template is rendered into html, that is, certain attribute values ​​are usually initialized before rendering into a view.

mounted: Called after the template is rendered into HTML, usually after the initialization page is completed, and then performs some required operations on the DOM node of the HTML.

In fact, the two are easier to understand. Created is usually used more often, while mounted is usually operated during the use of some plug-ins or components, such as the use of the plug-in chart.js: var ctx = document. getElementById(ID); usually there is this step, but if you write it into the component, you will find that you cannot perform some initial configuration on the chart in created. You must wait until the html is rendered before proceeding. Then mounted is the best choice. choice. Let’s look at an example (using components).

3. Example

Vue.component("demo1",{
        data:function(){
            return {
                name:"",
                age:"",
                city:""
            }
        },
        template:"<ul><li id=&#39;name&#39;>{{name}}</li><li>{{age}}</li><li>{{city}}</li></ul>",
        created:function(){
            this.name="唐浩益"
            this.age = "12"
            this.city ="杭州"
            var x = document.getElementById("name")//第一个命令台错误
            console.log(x.innerHTML);
        },
        mounted:function(){
            var x = document.getElementById("name")//第二个命令台输出的结果
            console.log(x.innerHTML);
        }
    });
    var vm = new Vue({
        el:"#example1"
    })

You can see the output as follows:


You can see both It was successfully rendered with the initial value assigned by created.

But at the same time, look at the console as follows:


You can see that the first one reported an error, which is actually because the id, getElementById, cannot be found. (ID) element was not found for the following reasons:

When created, the html in the view was not rendered, so if you directly operate the dom node of the html at this time, you will not be able to find the relevant elements.

In mounted, since the html has been rendered at this time, the dom node can be directly operated, so the result "Tang Haoyi" is output.

The above is my own summary of the difference between mounted and mounted. The writing is relatively simple. I will record it to deepen my impression.

This article is reproduced from: https://blog.csdn.net/xdnloveme/article/details/78035065

Recommended tutorial: "

JS Tutorial"

The above is the detailed content of The difference between mounted and created in Vue (detailed explanation with pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete