Performance: v-once for static content
This affects the rendering speed by skipping future updates for static parts of the UI, reducing CPU work during re-renders.
<template>
<div v-once>
<h1>{{ title }}</h1> <!-- title never changes -->
</div>
</template>
<script setup>
const title = 'Welcome to Vue'
</script><template>
<div>
<h1>{{ title }}</h1> <!-- title never changes -->
</div>
</template>
<script setup>
const title = 'Welcome to Vue'
</script>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Without v-once | Re-renders virtual DOM nodes every update | Triggers reflows if DOM changes | Paint cost depends on changes | [X] Bad |
| With v-once | Renders DOM nodes once, skips updates | No reflows from static content on updates | Paint cost only on initial render | [OK] Good |
v-once directive do in a Vue template?v-oncev-once tells Vue to render the element or component only one time and then never update it again, even if reactive data changes.v-once is about skipping future updates to improve performance for static content.v-once = Render once [OK]v-once means render once only [OK]v-once with event listenersv-once to a paragraph element?v-once are used as attributes without values unless they accept arguments or expressions.v-once without any value or binding syntax.Static text here
[OK]v-once as a plain attribute without value [OK]<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>v-once effect on renderingv-once directive renders the element only once and skips updates, so the initial value of title is used.title.value changes to 'Goodbye', but the <h1> does not update because of v-once.v-once = unchanged text [OK]v-once freezes initial render [OK]v-once<div v-once>
<p>Count: {{ count }}</p>
</div>
<script setup>
import { ref } from 'vue'
const count = ref(0)
setInterval(() => { count.value++ }, 1000)
</script>v-once on updatesv-once renders the element once and disables future updates, so changes to count won't reflect.count is reactive and setInterval updates it correctly; interpolation syntax is valid.v-once prevents updates to the count display -> Option Bv-once disables updates = Using v-once prevents updates to the count display [OK]v-once disables reactive updates [OK]setInterval is not calledv-once. Which of these is the best use case for v-once?v-once is usefulv-once is best for content that never changes after initial render to save update costs.v-once fits. User profile, live clock, and message list all change and need updates, so v-once is inappropriate.v-once [OK]v-once only for truly static content [OK]v-once to dynamic datav-oncev-once on frequently changing UIPass the test and this pattern moves unknown