Home > Web Front-end > JS Tutorial > Detailed explanation of the use of created in vue.js

Detailed explanation of the use of created in vue.js

php中世界最好的语言
Release: 2018-04-20 15:54:12
Original
1560 people have browsed it

This time I will bring you a detailed explanation of the use of created in vue.js. What are the precautions for using created in vue.js. The following is a practical case, let's take a look.

This is one of its life cycle hook functions, which is to call this function after a vue instance is generated. After a vue instance is generated, it must be bound to a certain html element, and then it must be 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.

Added:

##VThe difference between mounted and created in the ue 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: in the

template Called before rendering into html, that is, some 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 in the use of some plug-ins or the use of

components, such as the use of the plug-in chart.js. : var ctx = document.getElementById(ID); Usually there is this step, and if you write it into the component, you will find that you cannot perform some initial configuration of the chart in created. You must wait for this html It can only be done after rendering, so 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 everything is assigned in created It was successfully rendered with the 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 is 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

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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Webpack4.0 packaging optimization strategy summary_javascript skills

Key value string conversion JS method of json string

The above is the detailed content of Detailed explanation of the use of created in 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