Tag : lifecycle-hooks
Tag : 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.
Let’s add our code to the hooks and see the phases how they gets fired.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<!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> |
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.
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.
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.
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.
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.
1 2 3 4 |
// To fire update v1.$data.hello_message = "New message"; |
Then the changes get rendered on screen by actually updating the DOM object and fires the updated hook.
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.
1 2 3 4 |
// This can be invoked to destroy the object, which will fire the destroy hook v1.$destroy(); |
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