Consider this Vue component using conditional rendering with @if. What text will appear when the component first renders?
<template>
<div>
<p @if="showMessage">Hello, Vue learner!</p>
<button @click="showMessage = !showMessage">Toggle Message</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const showMessage = ref(false)
</script>Think about the initial value of showMessage and how @if controls rendering.
The showMessage variable starts as false. The @if directive only shows the paragraph if showMessage is true. So initially, the paragraph is not rendered, and no text appears.
count after clicking the button twice?This Vue component toggles a message and increments a count only when the message is shown. What is the value of count after clicking the button two times?
<template>
<div>
<p @if="show">Message shown</p>
<button @click="toggle">Toggle</button>
<p>Count: {{ count }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(false)
const count = ref(0)
function toggle() {
show.value = !show.value
if (show.value) {
count.value++
}
}
</script>Count increments only when the message becomes visible.
Initially, show is false and count is 0. Clicking once sets show to true and increments count to 1. Clicking again sets show to false, so count stays 1.
Vue 3.4+ introduced new control flow directives. Which of these options correctly shows a paragraph only if isVisible is true?
Vue 3.4+ uses @if as a new control flow directive replacing v-if.
In Vue 3.4+, the new @if directive is the correct way to conditionally render elements. v-if and v-show are older syntax. :if is invalid.
Look at this Vue component. Clicking the button does not show or hide the message. What is the cause?
<template>
<div>
<p @if="showMessage">Hello!</p>
<button @click="toggleMessage">Toggle</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
const showMessage = ref(false)
function toggleMessage() {
showMessage = !showMessage
}
</script>Remember how to update a ref value in Vue.
In Vue, ref variables are objects with a .value property. The code tries to reassign the entire ref instead of toggling showMessage.value. This causes the toggle to fail.
Which statement best explains why conditional rendering matters in Vue applications?
Think about how rendering fewer elements affects browser work.
Conditional rendering lets Vue skip creating or updating parts of the page that are not needed. This reduces CPU and memory use, making the app faster and smoother.