When is mounted called?
The mounted hook is called after the component:
- has been created,
- has rendered the virtual DOM,
- and has inserted the real DOM element into the document.
In simpler terms:
mountedis called when the component is fully present in the DOM and you can work with it.
What is available in mounted?
At this point, you already have:
- access to the real DOM
- access to
refs - reactive data and props
- computed
- watchers
- all child components are also mounted
So you can safely perform DOM manipulations.
Example:
vue
<template>
<input ref="inputEl">
</template>
<script>
export default {
mounted() {
this.$refs.inputEl.focus(); // works!
}
}
</script>When exactly is mounted called?
Phases:
- The hooks ran:
beforeCreate→created. - Vue created the virtual DOM.
beforeMountran.- Vue inserted the generated DOM into the page.
- Now
mountedis called.
What is usually done in mounted?
1. Working with the DOM directly:
js
this.$refs.canvas.getContext('2d')2. Initializing third-party libraries:
- charts (Chart.js, ECharts)
- carousels
- maps (Leaflet, Google Maps)
- tooltips
- datepickers
3. Working with setInterval / setTimeout
4. Connecting a WebSocket
(sometimes this is done in created, but mounted is safer for the UI)
5. API requests, if the data matters after the UI renders
(though this is usually done in created)
When should you NOT use mounted?
Do not use it for heavy operations
Because mounted is the moment when the UI becomes visible. If the hook is slow, the UI will "freeze".
Do not mutate data endlessly
Changing data right after mounted can trigger an extra render.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.