Tag : vue-js

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 : https://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram)

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.

Summary

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

VueJs Series with Nirmal : Difference between v-if and v-show

Welcome to the VueJs series with Nirmal.

v-if

The control structure followed by VueJs for the UI element rendering. v-if supports the control branching if..else if..else.

A quick example to render spans based on the value typed in the input.

It renders the “Fruits” and “Birds” span when typed “Fruits” and “Birds” in the input text respectively. Otherwise it renders “Animal” span.

v-show

A boolean control to manage display of an UI element.

As the “show_sky” variable used in this example, depending on that it either displays the span or hides it.

v-if vs v-show

  • Unlike v-if, v-show has no else part
  • v-if only renders the elements which satisfies the condition. But v-show, renders all elements and manages with css “display” attribute.

Video

The video contains the demonstration of v-if vs v-show

Hope this helps.

Categories: VueJs

Vue.JS – An introduction

What is Vue.Js

A progressive javascript framework for building the UI. It is built on top of Javascript. Mainly focuses on the View layer for interactive UI designs.

Any pre-requisite knowledge to learn it ?

HTML, CSS and Javascript should be the basic one to know to start with it.

A quick introduction to kick start

Hope this helps.

Please do comment.

Categories: VueJs

%d bloggers like this: