Home > Web Front-end > JS Tutorial > body text

The role of the created method in vue.js_vue.js

亚连
Release: 2018-05-28 10:56:45
Original
1659 people have browsed it

This article mainly introduces the role of the created method in vue.js and the difference between mounted and created. Friends in need can refer to it

This is one of its life cycle hook functions, which is a vue instance This function is called after being generated. After a vue instance is generated, it must be bound to an html element, and then compiled and then inserted into the document. Each stage will have a hook function to facilitate developers to handle different logic at different stages.

Generally, you can call ajax in the created function to obtain the data required for page initialization.

Instance life cycle

Each Vue instance must go through a series of initialization processes before being created. For example, the instance needs to configure data observer, compile the template, mount the instance to the DOM, and then update the DOM when the data changes. During this process, the instance will also call some life cycle hooks, which provides us with the opportunity to execute custom logic. For example, the created hook is called after the instance is created:

var vm = new Vue({
data: {
a: 1
},
created: function () {
// `this` 指向 vm 实例
console.log('a is: ' + this.a)
}
})
// -> "a is: 1"
Copy after login

There are also some other hooks that are called at different stages of the instance life cycle, such as mounted, updated, destroyed. The hook's this points to the Vue instance that called it. Some users may ask whether Vue.js has a concept of "controller"? The answer is, no. A component's custom logic can be distributed among these hooks.

Life cycle diagram

The following figure illustrates the life cycle of an instance. You don't need to understand everything right away, but it will help later.


Supplement:

VueThe difference between mounted and created in the life cycle

1. What is the 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: rendered into html in the template Called before, that is, some property values ​​are usually initialized before rendering into the 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. Let’s look at an example (using components).

3. Example

<span style="font-size: 14px;">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")/</span>/第二个命令台输出的结果<span style="font-size: 14px;"> 
   console.log(x.innerHTML); 
  } 
 }); 
 var vm = new Vue({ 
  el:"#example1" 
 })</span>
Copy after login

You can see the output as follows:

You can see that they are successfully rendered when created is assigned an initial value.

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 cannot be found, getElementById(ID) The 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.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Sample code for Vue to implement internal component carousel switching effect

Detailed explanation of operator overloading in Javascript

JavaScript implements a simple dynamic progress bar effect

The above is the detailed content of The role of the created method in vue.js_vue.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!