Life cycle and hook functions in Vue

Life cycle and hook functions in Vue

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, Dom mounting, rendering → updating → rendering, and uninstallation. We call this the life cycle of Vue. In simple terms, the process from creation to destruction of a Vue instance is the life cycle.

Throughout the life cycle of Vue, it provides a series of events that allow us to register js methods when events are triggered, allowing us to control the overall situation with our own registered js methods. In these event response methods, this directly points to the vue instance.

2. The life cycle of Vue

Life 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

Features: Automatically called, but their calling time nodes are different.

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:

beforeMount --- DOM is mounted this.$el---$el at this time is a "virtual" DOM ​​node

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:

  • beforeUpdate --- Before data is updated (---- before and after changes in data in the view layer)
  • updated --- After the data is updated (----the changes before and after the data in the view layer)

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:
  • Introduction to Vue life cycle and detailed explanation of hook functions
  • Detailed explanation of Vue3 life cycle hook function
  • How many stages are there in the Vue life cycle? What are the differences?
  • Let's talk about the vue life cycle hook functions and when they are triggered

<<:  Hide HTML elements through display or visibility

>>:  A brief analysis of the relationship between various Tomcat logs and the segmentation of catalina.out files

Recommend

A brief discussion on the design of Tomcat multi-layer container

Table of contents Container Hierarchy The process...

How to manage large file uploads and breakpoint resume based on js

Table of contents Preface Front-end structure Bac...

Causes and solutions for MySQL data loss

Table of contents Preface Problem Description Cau...

HTML form application includes the use of check boxes and radio buttons

Including the use of check boxes and radio buttons...

Detailed explanation of Mencached cache configuration based on Nginx

Introduction Memcached is a distributed caching s...

Detailed explanation of the use of Docker commit

Sometimes you need to install certain dependencie...

js implements a simple calculator

Use native js to implement a simple calculator (w...

JavaScript single thread and asynchronous details

Table of contents 1. Task Queue 2. To explain som...

Uniapp realizes sliding scoring effect

This article shares the specific code of uniapp t...

Introduction to installing and configuring JDK under CentOS system

Table of contents Preface Check and uninstall Ope...