Vue3 Fundamentals · Rendering & Templates

List rendering

List rendering with v-for

List rendering

A list in your data should be a list on the page: names, scores, search results. Writing one element per item by hand breaks the moment the data changes — you want the elements to follow the array.

Vue repeats an element with the v-for directive. Give it a name for each item and the collection to walk:

<ul>
  <li v-for="item in items">{{ item }}</li>
</ul>

Vue renders one <li> per entry in items, in order. The name before in is yours to choose; it is available inside the element, so you can interpolate it or bind it.

You can also read the index by listing a second name:

<li v-for="(item, index) in items">{{ index }}: {{ item }}</li>

v-for works on arrays and on objects. The rendered order matches the array's order. Your job is to render one list item per entry in an array.

Challenge

The list is hard-coded to a single row, so it cannot show the items it holds. Export an App component from App.vue that renders an unordered list with one <li> per entry in items, showing each item's text.

The Test mounts your component and counts the list items. The starter hard-codes one item, so the Test fails — repeat the <li> with v-for.

App.vue
<script setup>
const items = ['a', 'b', 'c'];
</script>

<template>
  <ul>
    <li>a</li>
  </ul>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.