Lifecycle hooks a bhíonn ag ligint duit cód a rith ag amanna ar leith i saol an chuid — cruthú, foluain go dtí an DOM, nuashonrú, agus baint. Sa Composition API is feidhmeanna iad a thugann tú ar an tshuim taobh istigh de setup.
Na príomh-hooks in ord
<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>
