Sometimes you need to access a template ref of a nested component. For example, you want to focus an input field in a child component from the parent component.
Let's take a look at how you can do this in Vue 3:
In the child component, we define a template ref called innerChildRef
and expose it to the parent component using defineExpose()
.
Now you can access this template ref in the parent component:
StackBlitz Demo
Try it yourself in the following StackBlitz project:
If you liked this Vue tip, follow me on X to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:
Nuxt Tip: How to Fix "Nuxt Instance Unavailable" Error
If you are using Nuxt 3, you probably already encountered the 'Nuxt Instance Unavailable' error. What is it, and how to fix it?
Pinia Tip: Simple History With Undo and Redo Functionality
Build a simple history with undo and redo functionality using the useRefHistory composable from VueUse.