0
0
Vueframework~5 mins

Template refs for DOM access in Vue - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a template ref in Vue?
A template ref is a way to directly access a DOM element or a component instance inside the Vue template using a special attribute called ref. It helps you interact with the element or component in your script.
Click to reveal answer
beginner
How do you define a template ref on an element in Vue 3 Composition API?
You add <code>ref="name"</code> to the element in the template, then in the script use <code>const name = ref(null)</code> and Vue will link the DOM element to this variable after rendering.
Click to reveal answer
intermediate
Why should you avoid manipulating the DOM directly in Vue?
Vue manages the DOM for you reactively. Direct DOM manipulation can cause conflicts or unexpected behavior. Template refs should be used carefully and mostly for things Vue can't handle, like focusing an input.
Click to reveal answer
beginner
How do you access a DOM element using a template ref in Vue 3?
After defining a ref in the template and script, you access the DOM element with name.value. For example, inputRef.value.focus() sets focus on the input element.
Click to reveal answer
intermediate
What is the difference between ref and reactive in Vue?
ref creates a reactive reference to a single value or DOM element, accessed with .value. reactive creates a reactive object with multiple properties. Template refs use ref for DOM access.
Click to reveal answer
How do you declare a template ref for a DOM element in Vue 3 Composition API?
AUse <code>ref="name"</code> in template and <code>const name = ref(null)</code> in script
BUse <code>v-model="name"</code> in template and <code>const name = reactive({})</code> in script
CUse <code>id="name"</code> in template and <code>document.getElementById('name')</code> in script
DUse <code>ref="name"</code> in template and <code>const name = reactive(null)</code> in script
What is the correct way to access the DOM element from a template ref named inputRef?
A<code>inputRef.value</code>
B<code>inputRef</code>
C<code>inputRef.dom</code>
D<code>inputRef.element</code>
When is it appropriate to use template refs in Vue?
ATo replace all reactive data binding
BTo declare global variables
CTo style elements with CSS
DTo directly manipulate DOM elements when Vue cannot handle it
Which Vue API is used to create a reactive reference for template refs?
Acomputed()
Breactive()
Cref()
Dwatch()
What will happen if you try to access a template ref before the component is mounted?
AIt will return the DOM element immediately
BIt will be null or undefined
CIt will throw a syntax error
DIt will automatically mount the component
Explain how to use template refs in Vue 3 to focus an input element after the component loads.
Think about linking template and script with ref, then using lifecycle to access DOM.
You got /3 concepts.
    Describe the difference between using template refs and reactive data in Vue.
    Consider when you want to change the UI vs when you want to reach the actual element.
    You got /4 concepts.