onMounted hook running before DOM is available
See original GitHub issueEnvironment
- Operating System:
Darwin
- Node Version:
v17.3.1
- Nuxt Version:
3.0.0-27447229.20f3171
- Package Manager:
yarn@1.22.17
- Bundler:
Vite
- User Config:
vite
,build
,ssr
- Runtime Modules:
-
- Build Modules:
-
Reproduction
Not needed. The Bug is very clear
Describe the bug
The screenshot below says it all. On first load (refresh) everything is fine and I’m able to run querySelector
and access the DOM just fine. However if I navigate to a different route then come back I get null
.
<template>
<div class="TheAdvantages">
<ul>
<li class="first" @click="$router.push('')"> I'm first </li>
</ul>
</div>
</template>
<script setup lang="ts">
onMounted(() => {
console.log('onMonuted Hook:')
console.log(
"document.querySelector('.first'): ",
document.querySelector('.first')
)
setTimeout(() => {
console.log('setTimeout 0ms')
console.log(
"document.querySelector('.first'): ",
document.querySelector('.first')
)
}, 0)
})
Additional context
No response
Logs
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:24
- Comments:32 (6 by maintainers)
Top Results From Across the Web
Composition API: Lifecycle Hooks - Vue.js
Registers a callback to be called after the component has been mounted. ... This hook can be used to access the DOM state...
Read more >Understanding Vue.js Lifecycle Hooks - DigitalOcean
The created hook runs before the templates and Virtual DOM have been mounted or rendered - you are able to access reactive data...
Read more >vue.js - mounted method is fired before data loaded - VueJS
After Mounted Hook (which means we can access DOM), there is also beforeUpdate and updated hooks. These hooks can be used when data...
Read more >A Guide to Understanding Vue Lifecycle Hooks - Fjolt
This hook runs immediately before rendering occurs. ... $el is now available, so you can now access and manipulate the DOM from Vue....
Read more >Lifecycle / onMount • Svelte Tutorial
There are a handful of functions that allow you to run code at key moments ... is onMount , which runs after the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Workaround - Disable Transitions
in every page where you care about onMounted running accurately add this inside
setup
:notes:
I had a similar issue where I could not call .focus() on a template ref in onMounted(). I found that the setTimeout hack does work, but it’s pretty messy, so I’m not really a fan of that.
However, I came across https://github.com/nuxt/framework/issues/2801 and found that if you set
<NuxtPage :key="$route.fullPath" />
in youapp.vue
file, it works as you would expect, so no need to setTimeout, etc.I have no idea why this would be the solution. @danielroe thoughts?