The v-once directive tells Vue to render an element only once and skip future updates. This helps improve performance for content that does not change.
v-once for static content in Vue
Introduction
Syntax
Vue
<element v-once>Static content here</element>
The v-once directive can be added to any HTML element or Vue component.
Vue will render the element and its children only once, then skip re-rendering it.
Examples
Vue
<h1 v-once>Welcome to my site</h1>
Vue
<div v-once><p>This text is static.</p><img src="logo.png" alt="Logo"></div>
Vue
<button v-once>Click me</button>
Sample Program
The h2 with v-once renders only once and does not update when message changes. The paragraph updates normally.
Vue
<template>
<div>
<h2 v-once>This title never changes</h2>
<p>{{ message }}</p>
<button @click="changeMessage">Change Message</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const message = ref('Hello!')
function changeMessage() {
message.value = 'Message changed!'
}
</script>Important Notes
Use v-once only for truly static content to avoid confusion.
It can improve performance by skipping unnecessary updates.
Dynamic content inside v-once will not update after first render.
Summary
v-once renders content only once and skips future updates.
Use it for static parts of your UI to boost performance.
Works on any element or component in Vue templates.
Practice
1. What does the
v-once directive do in a Vue template?easy
Solution
Step 1: Understand the purpose of
v-oncev-oncetells Vue to render the element or component only one time and then never update it again, even if reactive data changes.Step 2: Compare with other directives
Unlike event listeners or conditional rendering,v-onceis about skipping future updates to improve performance for static content.Final Answer:
Renders the element or component only once and skips future updates -> Option CQuick Check:
v-once= Render once [OK]
Hint: Remember:
v-once means render once only [OK]Common Mistakes:
- Confusing
v-oncewith event listeners - Thinking it makes content reactive
- Assuming it delays rendering
2. Which of the following is the correct syntax to apply
v-once to a paragraph element?easy
Solution
Step 1: Recall Vue directive syntax
Vue directives likev-onceare used as attributes without values unless they accept arguments or expressions.Step 2: Identify correct usage
The correct syntax is simplyv-oncewithout any value or binding syntax.Final Answer:
<p v-once>Static text here</p> -> Option AQuick Check:
Directive attribute without value =Static text here
[OK]
Hint: Use
v-once as a plain attribute without value [OK]Common Mistakes:
- Adding ="true" which is unnecessary
- Using binding syntax like :v-once
- Trying to call it like a function
3. Consider this Vue template snippet:
What will be displayed in the <h1> after clicking the button?
<template>
<div>
<h1 v-once>Title: {{ title }}</h1>
<button @click="changeTitle">Change Title</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const title = ref('Hello')
function changeTitle() {
title.value = 'Goodbye'
}
</script>What will be displayed in the <h1> after clicking the button?
medium
Solution
Step 1: Understand
Thev-onceeffect on renderingv-oncedirective renders the element only once and skips updates, so the initial value oftitleis used.Step 2: Analyze what happens on button click
When the button is clicked,title.valuechanges to 'Goodbye', but the <h1> does not update because ofv-once.Final Answer:
Title: Hello -> Option DQuick Check:
Static render withv-once= unchanged text [OK]
Hint: Remember
v-once freezes initial render [OK]Common Mistakes:
- Expecting reactive update despite
v-once - Thinking interpolation shows raw text
- Assuming runtime error on update
4. You wrote this Vue template:
But the displayed count never changes. What is the problem?
<div v-once>
<p>Count: {{ count }}</p>
</div>
<script setup>
import { ref } from 'vue'
const count = ref(0)
setInterval(() => { count.value++ }, 1000)
</script>But the displayed count never changes. What is the problem?
medium
Solution
Step 1: Identify effect of
v-onceon updatesv-oncerenders the element once and disables future updates, so changes tocountwon't reflect.Step 2: Check reactivity and code correctness
countis reactive andsetIntervalupdates it correctly; interpolation syntax is valid.Final Answer:
Usingv-onceprevents updates to the count display -> Option BQuick Check:
v-oncedisables updates = Usingv-onceprevents updates to the count display [OK]
Hint: Remember
v-once disables reactive updates [OK]Common Mistakes:
- Assuming reactivity is broken
- Thinking
setIntervalis not called - Blaming interpolation syntax
5. You want to optimize a Vue app by marking static parts with
v-once. Which of these is the best use case for v-once?hard
Solution
Step 1: Understand when
v-onceis usefulv-onceis best for content that never changes after initial render to save update costs.Step 2: Evaluate each option
Footer is static, sov-oncefits. User profile, live clock, and message list all change and need updates, sov-onceis inappropriate.Final Answer:
A static footer that never changes after page load -> Option AQuick Check:
Static content = usev-once[OK]
Hint: Use
v-once only for truly static content [OK]Common Mistakes:
- Applying
v-onceto dynamic data - Expecting reactive updates with
v-once - Using
v-onceon frequently changing UI
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
