{"id":6340,"date":"2018-08-20T10:27:37","date_gmt":"2018-08-20T10:27:37","guid":{"rendered":"https:\/\/9series-blog.staging9.com\/?p=6340"},"modified":"2026-03-03T10:47:39","modified_gmt":"2026-03-03T10:47:39","slug":"4-main-categories-vuejs-lifecycle","status":"publish","type":"post","link":"https:\/\/www.9series.com\/blog\/4-main-categories-vuejs-lifecycle\/","title":{"rendered":"4 Main Categories of Vuejs Lifecycle"},"content":{"rendered":"<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/4-Main-Categories-of-Vuejs-Lifecycle1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3347\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/4-Main-Categories-of-Vuejs-Lifecycle1.jpg\" alt=\"4 Main Categories of Vuejs Lifecycle\" width=\"800\" height=\"500\" \/><\/a><\/p>\n<p><strong>Definition<\/strong><br \/>\n<span style=\"font-weight: 400\">Vuejs is an open source JavaScript framework used to build user-interfaces. It can also <\/span><span style=\"font-weight: 400\">function as a web app framework that is capable of handling advanced single-page apps.<\/span><\/p>\n<p><strong>VUEJS Lifecycle<\/strong><br \/>\nIf you want to go deep in Vue, firstly you should be familiar with the component lifecycle.<\/p>\n<p>Life-cycle methods serve as an imagination as to how our built components work behind the scenes. They provide you with methods that enable you to trigger different actions at a different interface of a component\u2019s life-cycle. You can use the component\u2019s state and methods through an auto-bind property of life-cycle methods to your component.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/4-Main-Categories-of-Vuejs-Lifecycle.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3346\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/4-Main-Categories-of-Vuejs-Lifecycle.jpg\" alt=\"4 Main Categories of Vuejs Lifecycle\" width=\"800\" height=\"496\" \/><\/a><\/p>\n<p><strong>Categories of VUEJS Lifecycle<\/strong><br \/>\nBelow are different four categories of lifecycle :<br \/>\n1. Creation (Initialization)<br \/>\n2. Mounting (DOM Insertion)<br \/>\n3. Updating (Diff &amp; Re-render)<br \/>\n4. Destruction (Teardown)<\/p>\n<p><strong>Creation<\/strong><br \/>\nThis method allows you to perform actions before your component has even been added to the DOM. Unlike methods Use creation hooks if you need to set things up in your component both during client rendering and server rendering.<\/p>\n<p>Creation methods are implemented using <em><strong>beforeCreate()<\/strong><\/em> and <em><strong>created()<\/strong><\/em> hooks<strong>. <\/strong>You can\u2019t able to access to the DOM or the target mounting element (this.$el) inside of creation hooks.<\/p>\n<p><em><strong>beforeCreate()\u00a0: <\/strong><\/em>The <em><strong>beforeCreate()<\/strong><\/em> hook runs at first when a component is still in the non-reactive mode and events that happen during the component\u2019s lifecycle have yet not been set up. You can see in the examples below where x is undefined.<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/21.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3348\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/21.jpg\" alt=\"2(1)\" width=\"800\" height=\"318\" \/><\/a><\/p>\n<p><em><strong>created()\u00a0:\u00a0The created() <\/strong><\/em>hook takes place when Vue has set up events and data observation. Here, templates have not yet been mounted or rendered but events are active and access to reactive data is enabled. Look at the code block below:<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3349\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/3.jpg\" alt=\"3\" width=\"800\" height=\"310\" \/><\/a><\/p>\n<p><strong>Mounting(DOM Insertion)<\/strong><br \/>\nThis method is widely used when working with components. It allows you to access your component immediately before and after it is rendered the first time. It is used when you want to modify the DOM of your component immediately before or after the initial render.<\/p>\n<p>During server-side rendering, this method shouldn\u2019t be used for fetching data for components on initialization. For that purpose, you can use <em><strong>created()<\/strong><\/em> methods<\/p>\n<p><em><strong>beforeMount()\u00a0:<\/strong><\/em> This method is cited only after the template has been compiled and virtual DOM is updated by Vue. After this, the <strong><em>$el(element)<\/em><\/strong> property is added to the Vue instance, the native DOM is updated and the <em><strong>mounted()<\/strong><\/em><strong> method is cited.<\/strong><\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3350\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/4.jpg\" alt=\"4\" width=\"800\" height=\"207\" \/><\/a><\/p>\n<p><em><strong>mounted()\u00a0<\/strong><\/em><strong>:<\/strong> The <em><strong>mounted()<\/strong><\/em> method you will have full access to the reactive component, templates, and rendered DOM. Mounted is the most generally used lifecycle hook. It is usually used for collecting the data for your component and then altering the DOM to amalgamate other libraries and frameworks asides Vue.\u00a0 Let\u2019s see the code block below:<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/51.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3362\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/51.jpg\" alt=\"5\" width=\"800\" height=\"112\" \/><\/a><\/p>\n<p>Using the <em><strong>mounted()<\/strong><\/em> method to fetch this data from the DOM:<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/6.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3352\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/6.jpg\" alt=\"6\" width=\"800\" height=\"174\" \/><\/a><\/p>\n<p><strong>Updating<\/strong><br \/>\nUpdating methods are very useful for debugging purpose. This method is called whenever a reactive property used by your component changes or re-render occurs. The component\u2019s DOM will be updated when this hook is called allowing you to use the updated methods to execute operations dependant on the DOM.<\/p>\n<p><em>Note:<\/em>\u00a0<em>Don\u2019t use updating methods to change state, for that you can use\u00a0<a href=\"https:\/\/vuejs.org\/v2\/api\/#computed\" target=\"_blank\">computed properties<\/a>\u00a0or\u00a0<a href=\"https:\/\/vuejs.org\/v2\/api\/#watch\" target=\"_blank\">watchers<\/a>.\u00a0<\/em><\/p>\n<p><em><strong>beforeUpdate()\u00a0:<\/strong><\/em> This method is executed after the data changes on your component and the updated cycle starts exactly before the DOM is patched and re-rendered. It lets you obtain the new state of any kind of reactive data on your component prior to being rendered. Let\u2019s look at the code block below:<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/before.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3370\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/before.jpg\" alt=\"before\" width=\"800\" height=\"545\" \/><\/a><\/p>\n<p><em><strong>updated()\u00a0:<\/strong><\/em> The <strong><em>updated() <\/em><\/strong>method runs immediately after the data changes on your component and the re-rendering of DOM takes place. Let\u2019s have a look at the code block below:<\/p>\n<p>Interacting with our Vue instance:<br \/>\n<a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/updated1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3372\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/updated1.jpg\" alt=\"updated\" width=\"800\" height=\"523\" \/><\/a><\/p>\n<p><strong>Destruction<\/strong><br \/>\nLast and final stage of a component\u2019s life-cycle is Destruction method. In this it allows you to execute actions such as cleanup when your components have been destroyed and are performed when your components have been torn down and removed from the DOM.<\/p>\n<p><strong>beforeDestroy()\u00a0:<\/strong>\u00a0This method is performed before the components are destroyed. Here your components are still completely present and perfectly functional. If you want to cleanup events, <strong>beforeDestroy()<\/strong> is the best time to do that. Let\u2019s take a look at the code block below:<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/91.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3360\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/91.jpg\" alt=\"9\" width=\"800\" height=\"362\" \/><\/a><\/p>\n<p><strong><em>destroyed()<\/em> :<\/strong> This method is called only after your component has been destroyed, its directives have been unbound and its event listeners have been removed successfully. The <em><strong>destroyed()<\/strong><\/em> method allows you to do last minute cleanup or inform the remote server about the component being destroyed. Let\u2019s take a look at the code block below:<\/p>\n<p><a href=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/101.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3361\" src=\"https:\/\/9series-blog.staging9.com\/wp-content\/uploads\/2018\/08\/101.jpg\" alt=\"10\" width=\"800\" height=\"224\" \/><\/a><\/p>\n<p><strong>Closing Note<br \/>\n<\/strong><span style=\"font-size: 1rem\">Through this <\/span><a style=\"font-size: 1rem\" href=\"https:\/\/www.9series.com\/services\/vue-js-development.html\" target=\"_blank\">vuejs development services<\/a><span style=\"font-size: 1rem\"> life-cycle, you should be able to visualize the journey of a Vue instance and get an idea about which method gets initialized at which phase of coding as well as it&#8217;s easy to customize your own whenever the need arises with help of various hooks or methods. For more details, you can go through the Vue\u2019s documentation for the other methods and properties that can be used alongside with the life-cycle methods when you are creating your components.<\/span><\/p>\n<p><strong>More to learn here:<\/strong><br \/>\nhttps:\/\/alligator.io\/vuejs\/<br \/>\nhttps:\/\/github.com\/vuejs\/vue<\/p>\n<p><strong>Source:<\/strong><br \/>\nhttps:\/\/scotch.io\/tutorials\/demystifying-vue-lifecycle-methods#toc-creation<br \/>\nhttps:\/\/scotch.io\/tutorials\/demystifying-vue-lifecycle-methods#toc-mounting<br \/>\nhttps:\/\/scotch.io\/tutorials\/demystifying-vue-lifecycle-methods#toc-updating<br \/>\nhttps:\/\/vuejs.org\/v2\/guide\/<br \/>\ncodeingexplained.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Definition Vuejs is an open source JavaScript framework used to build user-interfaces. It can also function as a web app framework that is capable of handling advanced single-page apps. VUEJS&#8230;<\/p>\n","protected":false},"author":1,"featured_media":6341,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"dsgo_overlay_header":false,"dsgo_overlay_header_text_color":"","dsgo_overlay_skip_top_bar":false,"_designsetgo_exclude_llms":false,"footnotes":""},"categories":[1465,1466],"tags":[2263,2217],"class_list":["post-6340","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source-javascript-framework","category-vuejs","tag-open-source-javascript-framework","tag-vuejs"],"_links":{"self":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts\/6340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/comments?post=6340"}],"version-history":[{"count":1,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts\/6340\/revisions"}],"predecessor-version":[{"id":6352,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/posts\/6340\/revisions\/6352"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/media\/6341"}],"wp:attachment":[{"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/media?parent=6340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/categories?post=6340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9series.com\/blog\/wp-json\/wp\/v2\/tags?post=6340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}