📖 Scenario: You are building a simple Vue app that shows a list of fruits. You want to display only the fruits that are fresh.
🎯 Goal: Create a Vue component that uses v-for to loop over a list of fruits and uses v-if inside the loop to show only fresh fruits.
📋 What You'll Learn
Create a list of fruits with their freshness status
Add a variable to track the freshness filter
Use v-for to loop over the fruits
Use v-if inside the loop to show only fruits that match the freshness filter
💡 Why This Matters
🌍 Real World
Filtering lists based on user choices is common in shopping sites, dashboards, and apps showing data.
💼 Career
Understanding how to combine v-for and v-if correctly is essential for building dynamic Vue interfaces that respond to user input.
Progress0 / 4 steps
1
Create the fruits list
Create a fruits array in the data() function with these exact objects: { name: 'Apple', fresh: true }, { name: 'Banana', fresh: false }, { name: 'Cherry', fresh: true }.
Vue
Hint
Use ref from Vue to create a reactive array called fruits with the given objects.
2
Add freshness filter variable
Add a showFresh variable using ref and set it to true inside the <script setup>.
Vue
Hint
Use const showFresh = ref(true) to create a reactive boolean variable.
3
Use v-for with v-if inside the template
Inside the <ul>, add a <li> that uses v-for="(fruit, index) in fruits" and inside it use v-if="!showFresh || fruit.fresh" to show only fresh fruits when showFresh is true.
Vue
Hint
Use v-for with (fruit, index) in fruits and add v-if="!showFresh || fruit.fresh" on the same <li> tag.
4
Add a checkbox to toggle freshness filter
Above the <ul>, add an <input type="checkbox"> bound with v-model="showFresh" and a label that says Show only fresh fruits.
Vue
Hint
Use a <label> with an <input type="checkbox" v-model="showFresh"> to toggle the freshness filter.
Practice
(1/5)
1. In Vue, when using v-for and v-if on the same element, which happens first?
easy
A. v-if runs first, then v-for loops over the filtered items.
B. Both run simultaneously.
C. v-for runs first, then v-if filters the rendered items.
D. Neither runs; Vue throws an error.
Solution
Step 1: Understand Vue's rendering order
Vue processes v-for first to create the list of items.
Step 2: Apply v-if filtering
After the list is created, v-if decides which items to actually render.
Final Answer:
v-for runs first, then v-if filters the rendered items. -> Option C
Quick Check:
v-for before v-if = B [OK]
Hint: Remember: loop first, then filter with v-if [OK]
Common Mistakes:
Thinking v-if filters before looping
Assuming simultaneous execution
Believing Vue throws error with both directives
2. Which of the following is the correct syntax to use v-for with v-if on the same element in Vue?
easy
A. <li v-for="item in items"> <template v-if="item.active">{{ item.name }}</template> </li>
B. <li v-for="item in items" v-if="item.active">{{ item.name }}</li>
C. <li v-if="item.active" v-for=item in items>{{ item.name }}</li>
D. <li v-if="item.active"> <template v-for="item in items">{{ item.name }}</template> </li>
Solution
Step 1: Check directive order on the same element
Vue allows v-for and v-if on the same element; v-for should come first logically but order in code doesn't affect behavior.
Step 2: Validate syntax correctness
<li v-for="item in items" v-if="item.active">{{ item.name }}</li> correctly places both directives on the <li> element with proper syntax.
Final Answer:
<li v-for="item in items" v-if="item.active">{{ item.name }}</li> -> Option B
Quick Check:
Correct syntax with both directives on same element = C [OK]
Hint: Put v-for and v-if on same tag; v-for first is common [OK]
Common Mistakes:
Swapping v-for and v-if causing confusion
Using v-if inside v-for incorrectly
Placing v-for inside v-if template incorrectly
3. Given the Vue template:
<ul>
<li v-for="item in items" v-if="item.show">{{ item.name }}</li>
</ul>