Frontory

Open code, practice, and notes →

Download source with English comments · for reading

Vue · 15 Template Refs and nextTick

Series contents · Technology map

Stage: Composables and Store Lifetimes · Editable example · Storage ID: vue-step-15

Goals and learning outcomes

Prerequisites

Unit 15 builds on the work and experiment records from Unit 14.

Key concepts and explanations

DOM references

A template ref points to the connected DOM element or component. It can be null when a conditional element is absent.

Batched updates

Vue batches DOM updates. After changing state, check after nextTick before handling actual DOM dimensions or focus.

Practice setup

First check the execution scope guidance on the example screen. The following is the preparation environment for extending the assignment in a separate project. Use .vue SFCs and script setup in a separate Vue Vite project. Record package versions and the lockfile, and run on the development server. The built-in Vue examples use the h function and are not a general-purpose SFC compiler.

The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.

Practice tasks

  1. Create a conditional textarea and ref in src/EditorPanel.vue.
  2. After setting visibility to true, connect await nextTick and focus.
  3. Repeat showing and hiding 5 times, checking the active element and null handling.

Variables to observe

VariableBaselineChangeExpected change / hypothesis
Focus timingImmediately after changing stateAfter nextTickThe created textarea can receive focus.

Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.

Completion criteria

Self-check

  1. Does nextTick also wait for network completion?
  2. Can a computed result replace a DOM reference?

Official references

Linked example and execution scope

Example source · Runtime environments and limits

This is an experiment with actual Vue reactivity and render functions. Check SFC templates in an external compilation environment.

Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.

Next learning

Previous: Component Contracts and Watching · Next: Composable Lifetimes

Open code, practice, and notes →