--- name: template-refs description: Access DOM elements and child component instances directly --- # Template Refs Template refs provide direct access to DOM elements and child component instances. ## Basic Usage (3.5+) Use `useTemplateRef()` for type-safe refs: ```vue ``` ## Legacy Approach (Pre-3.5) Use a ref with matching name: ```vue ``` ## Refs in v-for Collect multiple refs into an array: ```vue ``` > Note: ref array order is not guaranteed to match source array order. ## Function Refs Use a function for full control: ```vue ``` ## Component Refs Access child component instance: ```vue ``` ### Exposing Component Properties By default, ` ``` ## Common Use Cases ### Focus Management ```vue ``` ### Scroll Control ```vue ``` ### Third-Party Library Integration ```vue ``` ## Important Notes 1. **Refs are null before mount** - access in `onMounted` or later 2. **Conditional refs can be null** - elements with `v-if` may not exist 3. **Use optional chaining** - `ref.value?.method()` for safety 4. **Avoid overusing** - prefer declarative approaches when possible