Rendering a List with v-for in Vue
📖 Scenario: You are building a simple Vue app that shows a list of fruits on a webpage. You want to display each fruit name as a list item.
🎯 Goal: Create a Vue component that uses v-for to render a list of fruit names inside an unordered list.
📋 What You'll Learn
Create a reactive array called
fruits with the exact values: 'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry'.Create a variable called
listId with the value 'fruit-list' to use as the id attribute of the list.Use
v-for with the variable fruit and index i to render each fruit inside an <li> element.Add the
:key attribute to each <li> using the index variable i.Set the
id attribute of the <ul> element to the variable listId.💡 Why This Matters
🌍 Real World
Rendering lists dynamically is common in web apps, like showing products, messages, or tasks.
💼 Career
Understanding <code>v-for</code> is essential for Vue developers to build interactive and efficient user interfaces.
Progress0 / 4 steps