VueJs Series with Nirmal : Lifecycle hooks

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

(Image source :

Let’s add our code to the hooks and see the phases how they gets fired.

beforeCreate (A new object born)

Vue object instantiated with the new method. It creates an object of 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 initialised.

created (Object born with default characteristics)

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

beforeMounted (Object taking shape to fit in DOM)

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

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 mounted hook.

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 beforeUpdated hook, I have added the following code. It changes the hello_message in the runtime by updating the DOM.

updated (Changes rendered in DOM)

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

beforeDestroy (Object is ready to die)

Just before the vue object gets destroyed and freed up from the memory, deforeDestroy hook gets fired and allow 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.

destroyed (Object died and removed from the memory)

destroyed hook gets invoked after successfully destroy of object.


We can use the life cycle hooks to add our customised code in different phases of vue object life span. It will help us controlling the flow during object creation to rendering in DOM, updation and deletion of object.

Hope this helps.

Happy coding

Categories: VueJs

%d bloggers like this: