Vue 3 v-for Directive
v-for The directive is used to render elements multiple times based on source data; it is the core directive for list rendering in Vue.
Basic Explanation
v-for The directive repeats rendering elements based on an array or object. It lets you repeat a group of similar elements and use values from the data source.
- Expected type: Array | Object | number | string | Iterable
- Purpose: list rendering, rendering elements multiple times based on source data.
Basic Syntax
Example
{{ item }}
{{ index }} - {{ item }}
{{ key }}: {{ value }}
{{ n }}
Use Cases
1. Array Iteration
Example
-
{{ index + 1 }}. {{ fruit }}
2. Object Array Iteration
Example
|
{{ person.id }} |
{{ person.name }} |
{{ person.age }} |
The Importance of key
It is strongly recommended to provide a unique key attribute for each loop item in v-for, which helps Vue update the list efficiently.
Example
{{ item.name }}
Vue 3 Built-in Directives