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 explain TypeScript generics in a simple way

Table of contents Overview What are Generics Buil...

MySQL 8.0.15 version installation tutorial connect to Navicat.list

The pitfalls 1. Many tutorials on the Internet wr...

How to find and delete duplicate rows in MySQL

Table of contents 1. How to find duplicate rows 2...

Optimization analysis of Limit query in MySQL optimization techniques

Preface In actual business, paging is a common bu...

How to create a trigger in MySQL

This article example shares the specific code for...

Detailed explanation of the marquee attribute in HTML

This tag is not part of HTML3.2 and is only suppo...

Several ways to implement CSS height changing with width ratio

[Solution 1: padding implementation] principle: I...

A brief analysis of the count tracking of a request in nginx

First, let me explain the application method. The...

How to use nginx as a proxy cache

The purpose of using cache is to reduce the press...

Vue2/vue3 routing permission management method example

1. There are generally two methods for Vue routin...

Simple implementation of html hiding scroll bar

1. HTML tags with attributes XML/HTML CodeCopy co...