In vue, the created hook function will be called after the instance is created. created is a life cycle hook function. After a vue instance is generated, there will be a hook function in each stage, which is convenient for different stages. To handle different logic, you can generally call ajax in the created function to obtain data.
The operating environment of this article: Windows 10 system, Vue version 2.9.6, DELL G3 computer.
The created method in vue.js is a life cycle hook function. This function will be called after a vue instance is 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 function is called after the instance is created:
var vm = new Vue({ data: { a: 1 }, created: function () { // `this` 指向 vm 实例 console.log('a is:', this.a) } })
There are also some other hooks that are called at different stages of the instance life cycle, such as mounted, updated, destroyed. This in the hook function points to the Vue instance that called it. Some children may ask whether Vue.js has the concept of "controller"? The answer is, no. A component's custom logic can be distributed among these hooks.
[Related recommendations: "vue.js Tutorial"]
The above is the detailed content of When does vue use created?. For more information, please follow other related articles on the PHP Chinese website!