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

HTML table tag tutorial (12): border style attribute FRAME

Use the FRAME property to control the style type ...

HTML+CSS project development experience summary (recommended)

I haven’t updated my blog for several days. I jus...

How to start jar package and run it in the background in Linux

The Linux command to run the jar package is as fo...

How to generate mysql primary key id (self-increment, unique and irregular)

Table of contents 1. Use the uuid function to gen...

How to use the Linux md5sum command

01. Command Overview md5sum - Calculate and verif...

Simple encapsulation of axios and example code for use

Preface Recently, when I was building a project, ...

Detailed tutorial on how to delete Linux users using userdel command

What is serdel userdel is a low-level tool for de...

Some suggestions for Linux system optimization (kernel optimization)

Disable swap If the server is running a database ...

Compile CPP files using G++ in Ubuntu

When I used g++ to compile the cpp file for the f...

Vue custom directive details

Table of contents 1. Background 2. Local custom i...

A brief discussion on the solution to excessive data in ElementUI el-select

Table of contents 1. Scenario Description 2. Solu...

Solution to the long delay of MySQL database master-slave replication

Preface The delay of MySQL master-slave replicati...