New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
this.$el and this.$refs not populated in mounted() hook on first client-side page render #8981
Comments
I did some more digging on this and discovered that if you remove After reading this StackOverflow answer, I suspect this may be because the components are imported differently in v2 of |
I love Nuxt, and it's a shame I can't take advantage of the |
I found this #8879. FYI. |
This solved my problem. |
@liqueflies It's not guaranteed to work - and we're currently waiting on vuejs/vue#11963. |
Closing since is duplicate of #8879. Explanation and workarounds: #8879 (comment) Also for clarification vue/11963 won't solve this issue. |
@pi0 I don't think that other issue exactly matches this one. As you shared, it does make senses for |
Actually, for async components, it is a different story. Until the actual component is being resolved, a placeholder is used in place. Please see this codepen using async components and ref with vue 2.x: https://codepen.io/pi0/pen/BaQEqbL Options to resolve this issue are:
Also related issue: vuejs/vue#2247 |
Sorry @pi0 and thank you for reply, Is something that should solve maybe at Thank you! |
Because with nuxt 2.14.x and components v1, we were always using loader that imports components synchronously (also in development). In v2, during development, we use async components without loader due to the performance impact it had to analyze components. This vue limitation is only when using global/async components (i.e with 2.15.x refs are available in
One of the 3 methods mentioned above. There is no guarantee async component gets loaded and rendered in exactly the next tick. (updated codepen for next tick)
Indeed. I'm still looking for a workaround or either fix it in upstream. But in meantime, you can either remove |
I would like to propose that this new behavior is potentially very confusing in a number of different circumstances. I just ran into another situation where the usage of async components led to a bug which was very hard to find. See the Code Sandbox link below for an example: In the example, when the root |
When navigating to a page on the client side, if the page is being navigated to for the first time from the client side and if the root element of the page being navigated to is another component, then
this.$el
in the page component is a comment instead of the other component andthis.$refs
is an empty object when accessed in themounted()
lifecycle hook.Versions
Reproduction
Link to Code Sandbox where you can see the issue.
Load the
/about
page first (refresh if needed to ensure the/index
page has not been loaded from the client side) and then navigate to the/index
page so that it is being rendered on the client for the first time.Steps to reproduce
pages/index.vue
file with the following code:pages/about.vue
file with the following code:components/Dummy.vue
file with the following code:/about
page first./index
page. Note in the console that in themounted()
lifecycle hook inindex.vue
,this.$el
is a comment andthis.$refs
is an empty object.What is Expected?
As far as all Vue documentation seems to indicate, by the time
mounted()
is calledthis.$el
should be the DOM element at the root of the template or the Vue component at the root of the template, andthis.$refs
should be populated. In a project I'm working on, I need to accessthis.$el
in themounted()
hook, but since it is a comment I am not able to.What is actually happening?
When navigating to a page on the client side, if the page is being navigated to for the first time from the client side and if the root element of the page being navigated to is another component, then
this.$el
in the page component is a comment instead of the other component andthis.$refs
is an empty object when accessed in themounted()
lifecycle hook.The text was updated successfully, but these errors were encountered: