onMounted()
Registers a callback to run after the component is mounted to the DOM.
Syntax
onMounted(callback): void Parameters
| Name | Type | Required | Description |
|---|---|---|---|
callback | () => void | No | Function executed once the component DOM exists. |
Returns
void — Nothing; registers the hook.
Examples
<script setup lang="ts">
import { ref, onMounted } from 'vue';
const el = ref<HTMLElement | null>(null);
onMounted(() => {
el.value?.focus();
});
</script>
<template><input ref="el" /></template>
Notes
Runs after the component's initial render, so template refs and the DOM are
available. Good for DOM measurement, focus, subscriptions, and initializing
third-party libraries. Pair teardown logic with onUnmounted.