Challenge - 5 Problems
Vue v-if Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is rendered with v-if and v-else?
Consider this Vue template snippet:
If the component's
<template>
<div>
<p v-if="showMessage">Hello!</p>
<p v-else>Goodbye!</p>
</div>
</template>If the component's
showMessage data property is false, what will be shown in the browser?Attempts:
2 left
💡 Hint
Remember that v-else shows content only when v-if is false.
✗ Incorrect
The v-if directive shows the element only if the condition is true. When showMessage is false, the v-else block runs, showing 'Goodbye!'.
❓ state_output
intermediate2:00remaining
How many paragraphs render with multiple v-if?
Given this Vue template:
If
<template>
<div>
<p v-if="count > 0">Positive</p>
<p v-if="count < 0">Negative</p>
<p v-if="count === 0">Zero</p>
</div>
</template>If
count is 0, how many <p> elements will be rendered?Attempts:
2 left
💡 Hint
Each v-if is independent here.
✗ Incorrect
Each v-if is separate. Only the condition count === 0 is true, so only the 'Zero' paragraph renders.
📝 Syntax
advanced2:00remaining
Which v-if usage causes a syntax error?
Which of these Vue template snippets will cause a syntax error when using
v-if?Attempts:
2 left
💡 Hint
Check how attribute values are quoted in HTML.
✗ Incorrect
In HTML, attribute values must be quoted. Option A misses quotes around isVisible, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does v-if not update when data changes?
A Vue component has this template:
And this script:
Why does the paragraph not appear after 1 second?
<template>
<div>
<p v-if="show">Shown</p>
</div>
</template>And this script:
const show = false
setTimeout(() => {
show = true
}, 1000)Why does the paragraph not appear after 1 second?
Attempts:
2 left
💡 Hint
Vue tracks reactive data to update the DOM.
✗ Incorrect
Variables must be reactive (like refs) for Vue to detect changes and update the DOM. A plain variable show is not reactive.
🧠 Conceptual
expert3:00remaining
What happens when v-if and v-for are used on the same element?
Consider this Vue template:
What is the behavior of this code?
<template>
<ul>
<li v-for="item in items" v-if="item.visible" :key="item.id">
{{ item.name }}
</li>
</ul>
</template>What is the behavior of this code?
Attempts:
2 left
💡 Hint
Think about the order Vue processes directives on the same element.
✗ Incorrect
Vue processes v-for first, creating a list of elements. Then v-if runs on each element to decide if it should render. So only items with item.visible true appear.