Home > Article > Web Front-end > 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='name'>{{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:
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!