nuxt v3.16.1 not rendering $refs when using LazyComponents
hey guys, im trying to "drilling" some $refs by using lazyComponents, how is the correct way to achieve this?
if u look in the images that i uploaded, It will not work because i'm using lazy load in components, if i remove lazy, works. But if i need to achieve the refs values using lazyComponents, what i need to do?




5 Replies
Hi @shun. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
When using lazy-loaded components (e.g., components prefixed with
You cannot access refs on lazy components until they are hydrated. Make sure to use an appropriate hydration strategy and only access the refs after hydration. Also, use
Lazy
in Nuxt 3), accessing $refs
can be tricky because the component may not be hydrated or rendered at the time you try to access its ref. This is especially relevant if you are using Nuxt's built-in lazy hydration strategies, which delay when the component becomes interactive and, therefore, when its refs are available.
Key points from the knowledge sources:
- Lazy components in Nuxt 3 are not always hydrated immediately; hydration can be delayed until the component is visible, idle, interacted with, or after a timeout, depending on the strategy you use. Until hydration occurs, refs to those components (and their internals) will not be available for access in the parent component. This is expected behavior and is part of how lazy hydration optimizes performance in Nuxt 3 Delayed (or Lazy) Hydration.
- If you need to access a ref inside a lazy component, you must ensure the component is hydrated before attempting to access its ref. For example, you could use the hydrate-on-visible
strategy and only access the ref after the component is visible and hydrated.
- When using refs with components (not plain HTML elements), you must use defineExpose
in the child component to expose internal refs to the parent Unable to access the element when using template refs with v-for in Vue3/Nuxt3 on a custom component.
What you can do:
1. Expose the ref in the child component:
2. Ensure the component is hydrated before accessing the ref:
- If you use a lazy hydration strategy (e.g., hydrate-on-visible
), only access the ref after the component is hydrated. You may need to watch for hydration or use an event/callback to know when it's safe to access the ref.
3. Access the ref in the parent after hydration:
Summary:You cannot access refs on lazy components until they are hydrated. Make sure to use an appropriate hydration strategy and only access the refs after hydration. Also, use
defineExpose
in the child component to make its refs available to the parent Delayed (or Lazy) Hydration Unable to access the element when using template refs with v-for in Vue3/Nuxt3 on a custom component.
If you need more granular control, you may need to listen for hydration or use a watcher to know when the ref becomes available.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@243884352629374980> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai how i can "watch" the hydration process? to can see when is completed?
Let me check the knowledge base for you again. One moment! :books:
----------------------
You can "watch" the hydration process of lazy components in Nuxt 3 by listening to the @hydrated event. All delayed (lazy) hydration components emit a @hydrated event when they are hydrated. You can use this event to trigger logic or simply log when hydration is complete.
Example:
This approach works for any of the lazy hydration strategies (e.g.,
hydrate-on-visible
, hydrate-on-idle
, etc.) and is the recommended way to detect when a lazy component has finished hydrating in Nuxt 3 Listening to Hydration Events.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@243884352629374980> Kapa.ai is still learning and improving, please let me know how I did by reacting below.