📖 Scenario: You are building a simple Vue 3 app that shows a message based on the current weather condition.
🎯 Goal: Create a Vue 3 component that uses v-if, v-else-if, and v-else directives to display different messages for weather conditions.
📋 What You'll Learn
Create a reactive variable called weather with the value 'sunny'.
Create a reactive variable called temperature with the value 25.
Use v-if to show 'It is sunny and warm!' when weather is 'sunny' and temperature is above 20.
Use v-else-if to show 'It is rainy today.' when weather is 'rainy'.
Use v-else to show 'Weather is unknown.' for all other cases.
💡 Why This Matters
🌍 Real World
Conditional rendering is common in web apps to show different content based on user data or app state, like showing different messages for weather or user status.
💼 Career
Understanding <code>v-if</code>, <code>v-else-if</code>, and <code>v-else</code> is essential for Vue developers to build dynamic and interactive user interfaces.
Progress0 / 4 steps
1
Set up the Vue component with reactive data
Create a Vue 3 component using <script setup>. Inside it, create a reactive variable called weather with the value 'sunny' and another reactive variable called temperature with the value 25.
Vue
Hint
Use ref from Vue to create reactive variables.
2
Add the v-if condition for sunny and warm weather
Inside the <template>, add a <p> tag that uses v-if to show the text 'It is sunny and warm!' only when weather is 'sunny' and temperature is greater than 20.
Vue
Hint
Use v-if with a condition combining weather and temperature.
3
Add the v-else-if condition for rainy weather
Below the v-if paragraph, add another <p> tag that uses v-else-if to show the text 'It is rainy today.' only when weather is 'rainy'.
Vue
Hint
Use v-else-if with a condition checking if weather equals 'rainy'.
4
Add the v-else condition for unknown weather
Add a final <p> tag below the others that uses v-else to show the text 'Weather is unknown.' for all other cases.
Vue
Hint
Use v-else without a condition to catch all other cases.
Practice
(1/5)
1. What does the v-else directive do in Vue?
easy
A. Runs only if the first v-if condition is true
B. Runs when all previous v-if and v-else-if conditions are false
C. Runs independently of any other condition
D. Runs only if the last v-else-if condition is true
Solution
Step 1: Understand v-if and v-else-if behavior
v-if runs if its condition is true; v-else-if runs if the previous v-if or v-else-if was false and its own condition is true.
Step 2: Understand v-else behavior
v-else runs only if all previous v-if and v-else-if conditions are false, acting as a fallback.
Final Answer:
Runs when all previous v-if and v-else-if conditions are false -> Option B
Quick Check:
v-else = fallback [OK]
Hint: Remember: v-else is the fallback after all conditions fail [OK]
Common Mistakes:
Thinking v-else runs independently
Confusing v-else with v-else-if
Assuming v-else requires a condition
2. Which of the following is the correct syntax for using v-else-if in Vue?
easy
A. <div v-else-if="condition">Content</div>
B. <div v-else-if>condition</div>
C. <div v-else-if:condition>Content</div>
D. <div v-else-if(condition)>Content</div>
Solution
Step 1: Recall Vue directive syntax
Vue directives use the format v-directive="expression" to bind conditions or values.
Step 2: Check each option's syntax
<div v-else-if="condition">Content</div> uses v-else-if="condition", which is correct. Options B, C, and D use invalid syntax forms.
Final Answer:
<div v-else-if="condition">Content</div> -> Option A
B. The v-else directive cannot be used after v-else-if
C. The v-if directive must have a closing tag
D. There should be no v-else after v-if
Solution
Step 1: Check v-else-if usage
v-else-if requires a condition to evaluate. Here, it is used without any condition, which is invalid syntax.
Step 2: Verify other directives
v-else can follow v-else-if, and v-if has proper closing tags, so no issues there.
Final Answer:
The v-else-if directive is missing a condition -> Option A
Quick Check:
v-else-if needs a condition [OK]
Hint: v-else-if always needs a condition in quotes [OK]
Common Mistakes:
Leaving v-else-if without condition
Thinking v-else can't follow v-else-if
Confusing v-if closing tags
5. You want to display a message based on a user's status using Vue directives. The statuses are: 'admin', 'editor', and others. Which template correctly uses v-if, v-else-if, and v-else to show 'Admin Access', 'Editor Access', or 'Guest Access' respectively?