Detailed explanation of Vue life cycle functions

Detailed explanation of Vue life cycle functions

Lifecycle Functions

Life cycle functions are also called: life cycle callback functions, life cycle functions, life cycle hooks

What is it: Vue helps us call some functions with special names at critical moments

The name of the life cycle function cannot be changed, but the specific content of the function is written by the programmer according to the needs

This in the lifecycle function refers to vm or component instantiation object

Common life cycle hooks

1. mounted : send ajax request, start timer, bind custom event subscription message and other functions [initialization operation]

2. beforeDestory : clear timers, unbind custom events, unsubscribe messages, etc. [beginning and end work]

About Vue destroy instance:

1. After destruction, no information can be seen with the help of Vue developer tools

2. Custom events will become invalid after destruction, but native DOM events are still valid

3. Generally, data will not be operated in beforeDestory, because even if the data is operated, the update process will no longer be triggered.

insert image description here

Please add a description of the image

 <div id="root">
        <h2 :style="{opacity}"> Welcome to learn Vue's life cycle</h2>
        <button @click="stop">Click me to stop the change</button>

    </div>
    <script>
        Vue.config.productionTip = false;
        const vm = new Vue({
            el: '#root',
            data: {
                opacity: 1,
            },
            methods: {
                stop() {
                    // clearInterval(this.timer)
                    this.$destroy();
                }
            },
            //After Vue completes template parsing and puts the real initial DOM element into the page (mounting is complete), call mounted
            mounted() {

                this.timer = setInterval(() => {
                    this.opacity -= 0.01;
                    if (this.opacity <= 0)
                        this.opacity = 1;

                }, 16)
            },
            beforeDestroy() {
                console.log('vm is about to travel to the west');
                clearInterval(this.timer)
            }
        })
    </script>

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Do you know the componentization in Vue life cycle?
  • Let's take a look at the life cycle of Vue
  • Detailed explanation of Vue life cycle and data sharing
  • Let's learn about Vue's life cycle
  • Commonplace talk about the life cycle of Vue
  • A brief discussion on the life cycle of Vue
  • Detailed explanation of Vue life cycle
  • Let's talk about the Vue life cycle in detail
  • Sort out the life cycle in Vue
  • Introduction to the life cycle in Vue

<<:  Nginx reverse proxy configuration to remove prefix case tutorial

>>:  A brief discussion on several situations where adding indexes to MySQL does not take effect

Recommend

How to implement JavaScript output of Fibonacci sequence

Table of contents topic analyze Basic solution Ba...

Vue implements table paging function

This article example shares the specific code of ...

Java imports data from excel into mysql

Sometimes in our actual work, we need to import d...

MySQL login and exit command format

The command format for mysql login is: mysql -h [...

Tutorial diagram of installing centos7.3 on vmware virtual machine

VMware Preparation CentOS preparation, here is Ce...

How to install tomcat8 in docker

1. Install tomcat8 with docker 1. Find the tomcat...

Solve the problem of MySQL using not in to include null values

Notice! ! ! select * from user where uid not in (...

How to achieve centered layout in CSS layout

1. Set the parent container to a table and the ch...

MySQL detailed summary of commonly used functions

Table of contents MySQL Common Functions 1. Numer...

Implementation of MySQL Shell import_table data import

Table of contents 1. Introduction to import_table...

Deleting two images with the same id in docker

When I created a Docker container today, I accide...

Detailed explanation of the difference between chown and chmod commands in Linux

In Linux system, both chmod and chown commands ca...

MySQL 5.7.21 installation and configuration method graphic tutorial (window)

Install mysql5.7.21 in the window environment. Th...