What Are the Lifecycle Methods of Vue.js?

Introduction

Vue.js, a JavaScript framework for building user interfaces, provides a set of lifecycle methods that allow you to hook into different stages of a component's life. These methods can be used to perform actions or operations at specific points in the component's lifecycle. Here are the main lifecycle methods in Vue.js:

  1. beforeCreate: This is the earliest point in the component's lifecycle. At this stage, the Vue instance has been initialized, but data observation and event/watcher setup haven't occurred yet. You generally won't have access to the reactive data or methods at this point.
  2. created: This is called after the Vue instance has been created. At this point, the data observation has been set up, and you can access reactive data and methods. However, the component has not been mounted to the DOM yet.
  3. beforeMount: This hook is called just before the component is about to be mounted to the DOM. The template has been compiled, and the initial render is about to happen. This is a good place to perform actions that need to occur before the component is visible.
  4. mounted: Called after the component has been mounted to the DOM. This is often used for tasks that require access to the mounted DOM elements, such as initializing third-party libraries or fetching additional data.
  5. beforeUpdate: Called when the data changes, just before the update to the DOM. This is a good place to perform additional operations before the re-render, such as manipulating data or cleaning up resources.
  6. updated: Called after a data change causes the component to re-render and the DOM to be updated. This is useful for performing actions that need to occur after a component has been re-rendered.
  7. beforeDestroy: Called just before a Vue instance is destroyed. This is the ideal place to clean up event listeners, subscriptions, or other resources to prevent memory leaks.
  8. destroyed: Called after a Vue instance has been destroyed. This is where you can perform final cleanup, such as clearing intervals, timeouts, or other resources that were set up during the component's lifecycle.
  9. activated: This is specific to components wrapped in <keep-alive>. It is called when a kept-alive component is activated (i.e., it enters the DOM). Useful for dynamic components that are toggled and need to perform specific actions when brought back to life.
  10. deactivated: Also specific to components wrapped in <keep-alive>. It is called when a kept-alive component is deactivated (i.e., it leaves the DOM).

Vue.js lifecycle methods with code examples.

new Vue({
  // Options
  beforeCreate() {
    console.log('beforeCreate hook');
    // Initialization logic before data observation and event/watcher setup
  },
  created() {
    console.log('created hook');
    // Initialization logic after data observation and event/watcher setup
  },
  beforeMount() {
    console.log('beforeMount hook');
    // Logic before mounting begins
  },
  mounted() {
    console.log('mounted hook');
    // Logic after the component has been mounted to the DOM
  },
  beforeUpdate() {
    console.log('beforeUpdate hook');
    // Logic before component re-renders
  },
  updated() {
    console.log('updated hook');
    // Logic after component re-renders
  },
  beforeDestroy() {
    console.log('beforeDestroy hook');
    // Cleanup logic before component destruction
  },
  destroyed() {
    console.log('destroyed hook');
    // Cleanup logic after component destruction
  },
});

In this example

  • beforeCreate is called before the Vue instance is initialized.
  • created is called after the Vue instance is created and data observation has been set up.
  • beforeMount is called before the component is mounted to the DOM.
  • mounted is called after the component is mounted to the DOM.
  • beforeUpdate is called before a data change causes the component to re-render.
  • updated is called after a data change causes the component to re-render.
  • beforeDestroy is called before the Vue instance is destroyed.
  • destroyed is called after the Vue instance is destroyed.

Summary

Understanding the lifecycle methods is crucial for effective Vue.js development. These hooks allow you to perform actions at different stages of a component's existence, facilitating tasks like initialization, cleanup, and managing component state.