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

Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)

Wildcard categories: %Percent wildcard: indicates...

Vue uses echart to customize labels and colors

This article example shares the specific code of ...

Front-end state management (Part 2)

Table of contents 1. Redux 1.1. Store (librarian)...

Zabbix's psk encryption combined with zabbix_get value

Since Zabbix version 3.0, it has supported encryp...

SQL group by to remove duplicates and sort by other fields

need: Merge identical items of one field and sort...

Deploy Confluence with Docker

1. Environmental requirements 1. Docker 17 and ab...

How to install WSL2 Ubuntu20.04 on Windows 10 and set up the docker environment

Enable WSL Make sure the system is Windows 10 200...

5 solutions to CSS box collapse

First, what is box collapse? Elements that should...

How to deploy SpringBoot project using Docker

The development of Docker technology provides a m...

Things to note when migrating MySQL to 8.0 (summary)

Password Mode PDO::__construct(): The server requ...

MySQL slow query and query reconstruction method record

Preface What is a slow query and how to optimize ...