1. What is the life cycle A Vue instance has a complete life cycle, which includes a series of processes such as creation, data initialization, template compilation, Throughout the life cycle of Vue, it provides a series of events that allow us to register 2. The life cycle of VueLife cycle function, also called hook function (life cycle hook === life cycle function === life cycle event) The life cycle functions in Vue usually appear in pairs. So we compare them in pairs and see the difference between them. 10 life cycle functions to remember! Specific use! 3. Lifecycle hook function
I took a picture from the official website: beforeCreate --- "before creation" of the Vue instance. Note: In this function, the data in the data center of Vue cannot be read. <script src="./js/vue.js"></script> <script> let vm = new Vue({ el:'#app', data:{ name:"Hahaha", num:1111 }, methods: { }, // Before the vue instance is created beforeCreate(){ console.log('beforeCreate'); console.log(this.name); } </script> The name of the output data center cannot be read: created --- After the vue instance is created, note: in this function, the data in the data center of vue can be identified <script src="./js/vue.js"></script> <script> let vm = new Vue({ el:'#app', data:{ name:"Hahaha", num:1111 }, // After the vue instance is created created(){ console.log("created"); console.log(this.name); } }) </script> View the results: Render the label at the view level: <div id="app"> <p>{{name}}</p> <p>{{num}}</p> </div> <script src="./js/vue.js"></script> <script> let vm = new Vue({ el:'#app', data:{ name:"Hahaha", num:1111 }, //Before dom is mounted beforeMount(){ console.log("beforeMount"); //View DOM elements console.log(document.body.querySelector("#app").innerHTML); } }) </script> Output results before dom mounting: mounted --- after DOM is mounted this.$el --- at this time $el is the "real" DOM node <script src="./js/vue.js"></script> <script> let vm = new Vue({ el:'#app', data:{ name:"Hahaha", num:1111 }, // After dom is mounted mounted(){ console.log("mounted"); console.log(document.body.querySelector("#app").innerHTML); } }) </script> View the output:
In the view layer, click to change the value of num to simulate data update and view the result: <div id="app"> <p id="num">{{num}}</p> <button @click="num++">Click data update (num+1)</button> </div> //Before data update beforeUpdate(){ console.log("beforeUpdate--before data update"); // View DOM elements console.log(document.body.querySelector("#num").innerHTML); }, //After data is updated updated(){ console.log("updated--After data updated"); // View DOM elements console.log(document.body.querySelector("#num").innerHTML); } At this time, when the data does not change, the effect cannot be seen in the console. When we click the button: This is the end of this article about the life cycle and hook functions in Vue. For more information about the life cycle hook functions in Vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Hide HTML elements through display or visibility
Unfortunately, the MYSQL_DATA_TRUNCATED error occ...
1. Download, install and configure mysql-8.0.15 1...
Table of contents Container Hierarchy The process...
Table of contents Preface Front-end structure Bac...
1. MYSQL installation directory Copy the code as ...
Table of contents Preface Problem Description Cau...
Including the use of check boxes and radio buttons...
Introduction Memcached is a distributed caching s...
Sometimes you need to install certain dependencie...
Use native js to implement a simple calculator (w...
Table of contents 1. Install Docker on CentOS 7.9...
Table of contents 1. Task Queue 2. To explain som...
1 Background Recently, I have been studying how t...
This article shares the specific code of uniapp t...
Table of contents Preface Check and uninstall Ope...