📖 Scenario: You are building a simple Vue app that shows a welcome message only when the user is logged in. This is common in real websites where some parts appear only if you are signed in.
🎯 Goal: Create a Vue component that conditionally shows a welcome message based on a logged-in status. You will learn how to use Vue's conditional rendering to control what the user sees.
📋 What You'll Learn
Create a reactive variable called isLoggedIn with initial value false.
Create a button that toggles isLoggedIn between true and false.
Use Vue's v-if directive to show a <p> with text Welcome back, user! only when isLoggedIn is true.
Use Vue 3 Composition API with <script setup> and ref.
💡 Why This Matters
🌍 Real World
Many websites show or hide parts of the page depending on whether a user is logged in. Conditional rendering helps create dynamic, user-friendly interfaces.
💼 Career
Understanding conditional rendering is essential for frontend developers to build interactive apps that respond to user actions and state changes.
Progress0 / 4 steps
1
Set up the reactive login state
Inside the <script setup> block, create a reactive variable called isLoggedIn using ref and set it to false.
Vue
Hint
Use import { ref } from 'vue' and then const isLoggedIn = ref(false).
2
Add a button to toggle login state
In the <template>, add a <button> that toggles isLoggedIn between true and false when clicked. Use the @click event with an inline arrow function.
Vue
Hint
Use <button @click="() => isLoggedIn.value = !isLoggedIn.value"> to toggle the value.
3
Use v-if to conditionally show welcome message
Below the button in the <template>, add a <p> element with text Welcome back, user! that only appears when isLoggedIn is true. Use Vue's v-if directive with isLoggedIn.
Vue
Hint
Use <p v-if="isLoggedIn.value">Welcome back, user!</p> to show the message only when logged in.
4
Add accessibility and finalize component
Add an aria-pressed attribute to the button that reflects the isLoggedIn state for accessibility. The attribute value should be isLoggedIn. This helps screen readers understand the toggle state.
Vue
Hint
Bind aria-pressed to isLoggedIn using :aria-pressed="isLoggedIn.value".
Practice
(1/5)
1. Why is conditional rendering important in Vue applications?
easy
A. It speeds up the Vue app by skipping all rendering.
B. It automatically styles components without CSS.
C. It controls what content is shown based on conditions, improving user experience.
D. It replaces the need for JavaScript event handlers.
Solution
Step 1: Understand conditional rendering purpose
Conditional rendering shows or hides parts of the page based on data or user actions.
Step 2: Connect to user experience
Showing relevant content only makes the app easier and friendlier to use.
Final Answer:
It controls what content is shown based on conditions, improving user experience. -> Option C
Quick Check:
Conditional rendering = control content display [OK]
Hint: Conditional rendering controls visible content based on conditions [OK]
Common Mistakes:
Thinking it styles components automatically
Believing it skips all rendering to speed app
Confusing it with event handling
2. Which of the following is the correct syntax for conditional rendering in Vue to show a paragraph only if isVisible is true?
easy
A.
Visible paragraph
B.
Visible paragraph
C.
Visible paragraph
D.
Visible paragraph
Solution
Step 1: Identify correct directive for conditional rendering
Vue uses v-if to conditionally render elements based on a boolean.
Step 2: Check syntax correctness
v-if="isVisible" correctly binds the condition to show the paragraph only if true.
Final Answer:
<p v-if="isVisible">Visible paragraph</p> -> Option A
Quick Check:
Use v-if for conditional rendering [OK]
Hint: Use v-if="condition" to show/hide elements [OK]
Common Mistakes:
Using v-for instead of v-if for conditions
Confusing v-show with v-if (v-show toggles visibility but renders always)
Using v-bind incorrectly for conditional rendering
What is wrong if none of the paragraphs show even when isActive and isPending are both false?
medium
A. The variables isActive and isPending must be true to render anything.
B. Nothing is wrong; the last v-else should show 'Inactive'.
C. You cannot use v-else-if after v-if.
D. The v-else is missing a condition and causes error.
Solution
Step 1: Review v-if, v-else-if, and v-else logic
Vue renders the first true condition block; if none are true, v-else runs.
Step 2: Check variables false case
Both isActive and isPending false means v-else block should show 'Inactive'.
Final Answer:
Nothing is wrong; the last v-else should show 'Inactive'. -> Option B
Quick Check:
v-else runs if all previous conditions false [OK]
Hint: v-else shows when all prior conditions are false [OK]
Common Mistakes:
Thinking v-else needs a condition
Believing v-else-if is invalid after v-if
Assuming no output if all conditions false
5. You want to display a list of users but only show those who are active. Which Vue template snippet correctly uses conditional rendering inside a v-for loop to achieve this?
hard
A.
<ul>
<li v-for="user in users">{{ user.name }}</li>
</ul>