📖 Scenario: You are building a simple Vue app that shows a welcome message only when a user is logged in. This is common in websites where content changes based on user status.
🎯 Goal: Create a Vue component that uses the v-if directive to conditionally display a welcome message when the user is logged in.
📋 What You'll Learn
Create a reactive variable isLoggedIn with initial value false.
Add a button to toggle the isLoggedIn state.
Use v-if to show a <p> tag with text Welcome back, user! only when isLoggedIn is true.
Ensure the button text changes to Log out when logged in and Log in when logged out.
💡 Why This Matters
🌍 Real World
Many websites show or hide content based on user login status. Using v-if helps control what users see dynamically.
💼 Career
Understanding conditional rendering with v-if is essential for Vue developers building interactive user interfaces.
Progress0 / 4 steps
1
Set up the reactive login state
Create a Vue component with a reactive variable called isLoggedIn initialized to false using the ref function from Vue.
Vue
Hint
Use import { ref } from 'vue' and then const isLoggedIn = ref(false).
2
Add a button to toggle login state
Add a <button> inside the <div> that toggles the isLoggedIn value when clicked. Use the @click event with a function that sets isLoggedIn.value = !isLoggedIn.value. The button text should be Log in when isLoggedIn is false and Log out when true, using interpolation.
Vue
Hint
Use @click="isLoggedIn.value = !isLoggedIn.value" on the button and interpolation for the text.
3
Use v-if to conditionally show welcome message
Inside the <div>, add a <p> tag with the text Welcome back, user!. Use the v-if directive on this <p> to show it only when isLoggedIn.value is true.
Vue
Hint
Use v-if="isLoggedIn.value" on the <p> tag.
4
Add accessibility and final touches
Add an aria-pressed attribute to the <button> that reflects the isLoggedIn.value state for accessibility. The attribute value should be isLoggedIn.value. This helps screen readers understand the toggle state.
Vue
Hint
Bind aria-pressed using :aria-pressed="isLoggedIn.value" on the button.
Practice
(1/5)
1. What does the v-if directive do in Vue?
easy
A. It conditionally renders an element based on a boolean expression.
B. It always shows the element but changes its color.
C. It loops through a list and renders elements.
D. It binds a click event to an element.
Solution
Step 1: Understand the purpose of v-if
v-if controls whether an element is added or removed from the page based on a condition.
Step 2: Compare options
Only It conditionally renders an element based on a boolean expression. describes conditional rendering. Others describe unrelated Vue features.
Final Answer:
It conditionally renders an element based on a boolean expression. -> Option A
Quick Check:
v-if controls rendering = C [OK]
Hint: v-if adds/removes elements based on true/false [OK]
Common Mistakes:
Confusing v-if with v-show which only toggles visibility
Thinking v-if changes styles instead of rendering
Mixing v-if with event handling directives
2. Which of the following is the correct syntax to use v-if in a Vue template?
easy
A.
Content
B.
Content
C.
Content
D.
Content
Solution
Step 1: Recall Vue directive syntax
Vue directives use the format v-if="expression" inside the tag.
Step 2: Check each option
Only
Content uses the correct attribute binding syntax with quotes and equals sign.
Final Answer:
<div v-if="isVisible">Content</div> -> Option B
Quick Check:
Correct directive syntax = B [OK]
Hint: v-if uses = and quotes for condition expression [OK]