Lifecycle hooks आपको component के जीवन के विशेष क्षणों में कोड चलाने देते हैं — निर्माण, DOM में माउंटिंग, अपडेटिंग, और हटाना। Composition API में ये वो फ़ंक्शन हैं जिन्हें आप setup के अंदर कॉल करते हैं।
क्रम में मुख्य hooks
<script setup>
import { onMounted, onUpdated, onUnmounted, ref } from "vue";
const data = ref(null);
onMounted(() => {
// component is now in the DOM — fetch data, access elements, init libraries
fetchData().then(d => (data.value = d));
});
onUpdated(() => {
// runs after the DOM re-renders due to a reactive change
});
onUnmounted(() => {
// component is being removed — CLEAN UP here
clearInterval(timer);
window.removeEventListener("resize", handler);
});
</script>
