Vue provides several lifecycle hooks in the Options API:
beforeCreate() - Runs before the component is initialized (data and props are not yet reactive).created() - Runs after the component is initialized, and reactive properties are available.beforeMount() - Runs before the component is inserted into the DOM.mounted() - Runs when the component is inserted into the DOM (ideal for API calls and event listeners).beforeUpdate() - Runs before the component re-renders due to data changes.updated() - Runs after the component re-renders.beforeUnmount() - Runs before the component is removed from the DOM.unmounted() - Runs after the component is removed from the DOM (cleanup operations should be performed here).<template>
<div>
<p>Message: {{ message }}</p>
<button @click="updateMessage">Update Message</button>
</div>
</template>
<script>
export default {
data() {
return {
message: "Hello Vue!"
};
},
beforeCreate() {
console.log("beforeCreate: Component is being initialized.");
},
created() {
console.log("created: Component initialized. Data is now reactive.");
},
beforeMount() {
console.log("beforeMount: Template is compiled but not yet in the DOM.");
},
mounted() {
console.log("mounted: Component has been mounted in the DOM.");
},
beforeUpdate() {
console.log("beforeUpdate: Data is about to change.");
},
updated() {
console.log("updated: Data has changed, and the DOM is updated.");
},
beforeUnmount() {
console.log("beforeUnmount: Component is about to be removed.");
},
unmounted() {
console.log("unmounted: Component has been removed from the DOM.");
},
methods: {
updateMessage() {
this.message = "Updated Vue Message!";
}
}
};
</script>
beforeUpdate() and updated().beforeUnmount() and unmounted() execute.In Vue 3, the Composition API introduces new ways to use lifecycle hooks with setup(). The hooks follow the same functionality but are imported separately.
onBeforeMount()onMounted()onBeforeUpdate()onUpdated()onBeforeUnmount()onUnmounted()<template>
<div>
<p>{{ message }}</p>
<button @click="updateMessage">Update Message</button>
</div>
</template>
<script>
import { ref, onMounted, onUpdated, onUnmounted } from "vue";
export default {
setup() {
const message = ref("Hello Vue!");
const updateMessage = () => {
message.value = "Updated Vue Message!";
};
onMounted(() => {
console.log("onMounted: Component is mounted.");
});
onUpdated(() => {
console.log("onUpdated: Component has been updated.");
});
onUnmounted(() => {
console.log("onUnmounted: Component is removed.");
});
return { message, updateMessage };
}
};
</script>
onMounted() runs when the component is added to the DOM.onUpdated() runs when the state updates (e.g., changing the message).onUnmounted() executes when the component is removed from the DOM.| Hook | Use Case |
|---|---|
mounted() |
Fetching API data, setting up event listeners |
beforeUpdate() |
Running logic before DOM updates |
updated() |
Performing actions after the DOM updates |
beforeUnmount() |
Cleanup tasks before component removal |
unmounted() |
Removing event listeners, clearing intervals |
mounted(), updated(), and unmounted().onMounted(), onUpdated(), and onUnmounted().mounted() for fetching data, beforeUnmount() for cleanup, and updated() for handling DOM updates.