Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Vue.js Series: Lifecycle Hooks

DZone's Guide to

Vue.js Series: Lifecycle Hooks

A developer offers a discussion of various methods fellow web developers can use throughout the lifecycle of a Vue.js application.

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

Lifecycle hooks are the defined methods which get executed in a certain stage of the Vue object lifespan. Starting from the initialization, to when it gets destroyed, the object follows different phases of life. Here is a famous diagram indicating the hook sequence.

(Image source: https://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram)

Let's add our code to the hooks and see the phases of how they get fired.

<!DOCTYPE html >
<html>
<head>
<div id='div1' v-bind:title="div_title">
{{hello_message}}
</div>
</head>

<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var v1 = new Vue({
el: "#div1",
data: {
hello_message: "Hello, there welcome to VueJS world",
div_title: "This is my intro div",
},
beforeCreate: function(){ 
alert('Before Create');
},
created: function(){ 
alert('Created');
},
beforeMount: function(){ 
alert('Before Mount');
},
mounted: function(){ 
alert('Mounted');
},
beforeUpdate: function(){ 
alert('Before Update');
},
updated: function(){ 
alert('Updated');
},
beforeDestroy: function(){ 
alert('Before Destroy');
},
destroyed: function(){ 
alert('Destroyed');
}
});

// To fire update
v1.$data.hello_message = "New message";

// This can be invoked to destroy the object, which will fire the destroy hook
//v1.$destroy();
</script>
</body>
</html>

beforeCreate (A New Object Is Born)

A Vue object is instantiated with the new method. It creates an object of the Vue class to deal with the DOM elements. This phase of life of the object can be accessed by the  beforeCreated hook. We can insert our code in this hook to be executed before the object gets initialized.

Image title

created (Object Born With Default Characteristics)

In this phase of life, the object and its events are fully initialized. created is the hook to access this phase and to write code.

Image title

beforeMounted (Object Taking Shape to Fit in the DOM)

This hook is called beforeMounted. In this phase, it checks if any template is available in the object to be rendered in the DOM. If no template is found, then it considers the outer HTML of the defined element as a template.

Image title

Mounted (DOM Is Ready and Placed Inside the Page)

Once the template is ready. It fits the data into the template and creates the renderable element. Replaces the DOM element with this new data filled element. It all happens in themounted hook.
Image title

beforeUpdate (Changes Made and Yet Getting Ready to Update the DOM)

Upon changes made by the external event/user input this hook, i.e. beforeUpdate, gets fired before the changes reflecting the original DOM element.

To fire the beforeUpdated hook, I have added the following code. It changes the hello_message in the runtime by updating the DOM.

// To fire update
v1.$data.hello_message = "New message";

updated (Changes Rendered in the DOM)

Then the changes get rendered on the screen by actually updating the DOM object and fires the updated hook.
Image title

beforeDestroy (Object Is Ready to Die)

Just before the Vue object gets destroyed and freed up from the memory, the deforeDestroy hook gets fired and allows us to handle our custom code in it.

In order to fire this hook, I have added the following code to destroy the Vue object.

// This can be invoked to destroy the object, which will fire the destroy hook
v1.$destroy();

destroyed (Object Died and Removed From the Memory)

The destroyed hook gets invoked after successfully running destroy on the object.

Summary

We can use the lifecycle hooks to add our customized code in different phases of the Vue object lifespan. It will help us control the flow during object creation for rendering in the DOM, and updating and deleting the object.

Hope this helps.

Happy coding!

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:
web dev ,vue.js ,lifecycle management ,application development lifecycle ,web application development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}