Module: Vue Directives

v-for

Vue Directives: v-for

The v-for directive is used to render a list of items based on an array or an object. It's a fundamental directive for displaying dynamic data in Vue.js.

Rendering Lists from an Array

The most common use case is iterating over an array.

Syntax:

<template>
  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Cherry' }
      ]
    }
  }
}
</script>

Explanation:

  • v-for="item in items": This iterates over the items array. In each iteration, the current element is assigned to the variable item.
  • :key="item.id": The key attribute is crucial for Vue's efficient rendering. It helps Vue track each element in the list, especially when the list changes (e.g., items are added, removed, or reordered). Use a unique identifier for each item (like an id) as the key. If your items don't have a natural unique identifier, you can use the index (see below, but be aware of the caveats).
  • {{ item.name }}: This displays the name property of the current item.

Output:

<ul>
  <li>Apple</li>
  <li>Banana</li>
  <li>Cherry</li>
</ul>

Accessing the Index:

You can also access the index of the current item within the loop using the following syntax:

<template>
  <ul>
    <li v-for="(item, index) in items" :key="item.id">
      {{ index + 1 }}. {{ item.name }}
    </li>
  </ul>
</template>

Explanation:

  • v-for="(item, index) in items": This iterates over the items array, assigning the current element to item and its index to index.

Output:

<ul>
  <li>1. Apple</li>
  <li>2. Banana</li>
  <li>3. Cherry</li>
</ul>

Important Note about using Index as Key:

While using the index as the key is sometimes convenient, it's generally not recommended if the list is dynamic (items can be added, removed, or reordered). Using the index as the key can lead to unexpected behavior and performance issues because Vue might not correctly track changes to the list. Always prefer a unique identifier if available.

Rendering Lists from an Object

You can also use v-for to iterate over the properties of an object.

Syntax:

<template>
  <ul>
    <li v-for="(value, key, index) in myObject" :key="key">
      {{ index + 1 }}. {{ key }}: {{ value }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      myObject: {
        name: 'John Doe',
        age: 30,
        city: 'New York'
      }
    }
  }
}
</script>

Explanation:

  • v-for="(value, key, index) in myObject": This iterates over the properties of the myObject object.
    • value: The value of the current property.
    • key: The key (property name) of the current property.
    • index: The index of the current property (starting from 0).
  • :key="key": The key attribute is set to the property name (key), which is unique for each property in the object.

Output:

<ul>
  <li>1. name: John Doe</li>
  <li>2. age: 30</li>
  <li>3. city: New York</li>
</ul>

v-for with a Range

You can use v-for to repeat a template a specific number of times.

Syntax:

<template>
  <div>
    <span v-for="n in 10" :key="n">
      {{ n }}
    </span>
  </div>
</template>

Explanation:

  • v-for="n in 10": This iterates from 1 to 10, assigning each number to the variable n.
  • :key="n": The key attribute is set to the current number n.

Output:

<div>
  <span>1</span>
  <span>2</span>
  <span>3</span>
  <span>4</span>
  <span>5</span>
  <span>6</span>
  <span>7</span>
  <span>8</span>
  <span>9</span>
  <span>10</span>
</div>

Important Considerations

  • key Attribute: Always provide a unique key attribute when using v-for. This is essential for Vue's virtual DOM to efficiently update the list.
  • Performance: For very large lists, consider using techniques like pagination or virtualization to improve performance.
  • Avoid Modifying the Array Directly: Avoid directly modifying the array being iterated over within the v-for loop. This can lead to unexpected behavior. Instead, use Vue's reactive data binding and methods to update the array.
  • Nested v-for Loops: You can nest v-for loops to iterate over multi-dimensional data structures. Ensure each nested loop has a unique key.