v-once do in this Vue component?<template>
<div>
<h1 v-once>Hello, {{ name }}!</h1>
<button @click="changeName">Change Name</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const name = ref('Alice')
function changeName() {
name.value = 'Bob'
}
</script>
What will be displayed inside the <h1> after clicking the button?<template>
<div>
<h1 v-once>Hello, {{ name }}!</h1>
<button @click="changeName">Change Name</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const name = ref('Alice')
function changeName() {
name.value = 'Bob'
}
</script>The v-once directive tells Vue to render the element only once and skip future updates. So, even if the name changes, the content inside the <h1> does not update and stays as initially rendered.
computedValue updated with v-once?<template>
<p v-once>Computed: {{ computedValue }}</p>
<button @click="increment">Increment</button>
</template>
<script setup>
import { ref, computed } from 'vue'
const count = ref(0)
const computedValue = computed(() => count.value * 2)
function increment() {
count.value++
}
</script>
How many times will the computedValue be updated and reflected in the UI after clicking the button 3 times?<template>
<p v-once>Computed: {{ computedValue }}</p>
<button @click="increment">Increment</button>
</template>
<script setup>
import { ref, computed } from 'vue'
const count = ref(0)
const computedValue = computed(() => count.value * 2)
function increment() {
count.value++
}
</script>The v-once directive renders the element only once and disables future updates. So, even though computedValue changes, the UI inside the <p> does not update after the first render.
v-once to optimize static content?v-once to optimize rendering?Applying v-once on each <li> inside the v-for loop ensures each list item is rendered once and not updated later. Option A correctly places v-once on the <li> with a :key. Option A applies v-once on the whole <ul>, but the list is dynamic and Vue expects keys on children for updates. Option A misses keys, which is required for v-for. Option A is invalid as v-once on a comment does nothing.
v-once after state change?<template>
<div>
<p v-once>Static message: {{ message }}</p>
<button @click="updateMessage">Update</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const message = ref('Hello')
function updateMessage() {
message.value = 'Goodbye'
}
</script>
Why does the text inside <p> not change after clicking the button?<template>
<div>
<p v-once>Static message: {{ message }}</p>
<button @click="updateMessage">Update</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const message = ref('Hello')
function updateMessage() {
message.value = 'Goodbye'
}
</script>The v-once directive tells Vue to render the element only once and skip all future updates. So, even though message changes, the content inside the <p> does not update.
v-once in Vue components?v-once in Vue templates.v-once is used to optimize performance by telling Vue to render an element only once and skip all future updates. This is useful for static content that does not change during the component's lifecycle.