Using v-for with index in Vue
📖 Scenario: You are building a simple Vue app that shows a list of fruits. You want to display each fruit with its position number in the list.
🎯 Goal: Create a Vue component that uses v-for to loop over a list of fruits and display each fruit's name along with its index number starting from 1.
📋 What You'll Learn
Create a reactive array called
fruits using ref with the exact array: ['Apple', 'Banana', 'Cherry', 'Date']Add a
v-for directive to loop over fruits with variables fruit and indexDisplay the index number starting from 1 before each fruit name
Use a unique
key attribute with the index in the v-for loop💡 Why This Matters
🌍 Real World
Displaying lists with position numbers is common in apps like to-do lists, rankings, or menus.
💼 Career
Understanding v-for with index and keys is essential for building dynamic lists in Vue applications.
Progress0 / 4 steps