ref Template Reference

Last updated: 2026-10-22

ref Template Reference

The ref attribute names an element or component for script access via $refs or ref()/useTemplateRef().

GroupBuilt-ins
KindSpecial Attribute
ScopeTemplate
Vue Version3.0+
SSR SupportYes

📝 Syntax

<input ref="inp">
<child-comp ref="child"></child-comp>

⚙️ Arguments

propsA string name (auto-collected by same-name refs); arrays inside v-for

▶ Example

Edit the code below and see the live result:

Output:

The input is focused after mount.

💡 Key Points

ℹ️ Note Composition style: declare ref(null) and use the same name, or useTemplateRef in 3.5+.
⚠️ Warning Null before mount — guard access.
💡 Tip Prefer data-driven code; reach for refs only for focus, measurement and library mounting.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q Does the ref attribute clash with ref()?
A No — the attribute is a marker; ref() is the reactive API.
Q Can several elements be referenced?
A Refs inside v-for produce arrays (order not guaranteed).
Q When should it be avoided?
A Avoid it when state and events express the logic.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏