Home Web Front-end JS Tutorial How to understand the life cycle (hook function) in vue

How to understand the life cycle (hook function) in vue

Apr 18, 2019 pm 03:57 PM

The Vue life cycle (hook function) represents the process from creation to destruction of the Vue instance object. The Vue life cycle hook function is actually the option of the Vue instance. The values ​​of these options are all functions, which represent the various stages of the instance's life from birth to death. As long as this stage is reached, it will be automatically triggered.

How to understand the life cycle (hook function) in vue

[Recommended course: Vue Tutorial]

What does the life cycle of Vue refer to?

In layman's terms, the life cycle of Vue means that after the web page we write with Vue runs in the browser, the code we write must be executed in memory. For example, we all write var vm = new Vue();, which means new creates a Vue instance. From the creation of this instance until the instance dies when we close the browser, during this period of time, what does the Vue framework do, what does the Vue instance do, what is done first, what is done later, and what is the relationship between this series of things? Yes, this is the life cycle of Vue.

Vue’s life cycle is divided into three phases: creation phase, running phase, and destruction phase.

In the picture, I have marked each part of the life cycle and made some necessary explanations.

How to understand the life cycle (hook function) in vue

Process explanation (12 steps correspond to the operations in the picture):

1. Generate a Vue instance and execute the hook function beforeCreate( ). [Before instance creation]

2. Initialize the instance.

3. Mount the instance members to the view model and execute the hook function created(). [After the instance is created]

4. Determine whether there is an el object [the el object is used to indicate which area the view we control is].

5. If there is an el object, determine whether a template is used.

6. If a template is used, follow the method of compiling the template. If not, render the view area controlled by el as a template. Execute the hook function beforeMount(). [Before the instance is mounted]

7. Replace the original el view area with the changed new el view area. Execute the hook function Mounted() [after the instance is mounted].

8. Enter the running phase. The running phase is to perform some operations and execute the hook function beforeUpdate(). [Before data update]

9. After the operation is completed, render the data to the page and execute the hook function updated(). [After data update]

10. Enter the destruction phase and execute the hook function beforeDestroy() [Before instance destruction]

11. Perform destruction and disassemble the monitor, subcomponent and event listener .

12. The destruction is completed and the hook function destroyed() is executed. [After instance destruction]

  • The hook functions in the life cycle are events that Vue must execute during its life cycle. These events are actually functions.

  • Of course these events allow us programmers to write code so that when Vue's life cycle reaches this point, we can perform the operations we want.

  • The six hook functions in the creation phase and destruction phase of an instance are always executed once. Once executed, it will not be executed again.

Mentioned in the figure: We can access the instance members we defined only after the init Events are executed in the Vue life cycle, and this point is also the earliest point where the instance members can be accessed. To verify this, let's look at a piece of code.

<body>
    <div id="app"></div>
    //这里的路径为本机上的vue.js路径
    <script src="./lib/vue.js"></script>
    <script>
        var vm = new Vue({
            el : &#39;#app&#39;,
            data : {
                msg : &#39;我是初始值&#39;
            },
            methods : {
                show : function(){
                    console.log(this.msg);
                }
            },
            beforeCreate(){
                console.log(this.msg);
            },
            created(){
                console.log(this.msg);
            }
        });
    </script>
</body>
Copy after login

The result is as shown in the figure:

How to understand the life cycle (hook function) in vue

You can see that during beforeCreate(), we output undefined, and after created(), we output The value of msg.

This shows that the instance members of Vue are mounted on our vm only after they are created, so you can access our instance members by accessing them after they are created.

The above is the detailed content of How to understand the life cycle (hook function) in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

10 Ways to Instantly Increase Your jQuery Performance 10 Ways to Instantly Increase Your jQuery Performance Mar 11, 2025 am 12:15 AM

This article outlines ten simple steps to significantly boost your script's performance. These techniques are straightforward and applicable to all skill levels. Stay Updated: Utilize a package manager like NPM with a bundler such as Vite to ensure

Using Passport With Sequelize and MySQL Using Passport With Sequelize and MySQL Mar 11, 2025 am 11:04 AM

Sequelize is a promise-based Node.js ORM. It can be used with PostgreSQL, MySQL, MariaDB, SQLite, and MSSQL. In this tutorial, we will be implementing authentication for users of a web app. And we will use Passport, the popular authentication middlew

How to Build a Simple jQuery Slider How to Build a Simple jQuery Slider Mar 11, 2025 am 12:19 AM

This article will guide you to create a simple picture carousel using the jQuery library. We will use the bxSlider library, which is built on jQuery and provides many configuration options to set up the carousel. Nowadays, picture carousel has become a must-have feature on the website - one picture is better than a thousand words! After deciding to use the picture carousel, the next question is how to create it. First, you need to collect high-quality, high-resolution pictures. Next, you need to create a picture carousel using HTML and some JavaScript code. There are many libraries on the web that can help you create carousels in different ways. We will use the open source bxSlider library. The bxSlider library supports responsive design, so the carousel built with this library can be adapted to any

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

See all articles