Vue.js Loop Statements
In Vue, loop statements are mainly implemented through the v-for directive, used to iterate over arrays or objects, generating a corresponding number of elements.
Use the v-for directive on an element to render elements in a loop based on the source data array or object.
Iterating over an array:
v-for="(item, index) in items"
Iterating over an object:
v-for="(value, key, index) in object"
The role of keyUsing :
v-forWhen rendering a list, you must provide a uniquekeyattribute, so that Vue can recognize the uniqueness of each item and perform efficient DOM updates.Nested loops: Multiple can be nested
v-forto render multi-dimensional arrays or object structures.
v-for can bind data to an array to render a list:
v-for Example
<ol>
<li v-for="site in sites">
{{ site.text }}
</li>
</ol>
</div>
<script>
const app = {
data() {
return {
sites: [
{ text: 'Google' },
{ text: 'Example' },
{ text: 'Taobao' }
]
}
}
}
Vue.createApp(app).mount('#app')
</script>
Try it yourself »
v-for also supports an optional second parameter, whose value is the index of the current item:
v-for Example
index is the index value of the list item:
<ol>
<li v-for="(site, index) in sites">
{{ index }} -{{ site.text }}
</li>
</ol>
</div>
<script>
const app = {
data() {
return {
sites: [
{ text: 'Google' },
{ text: 'Example' },
{ text: 'Taobao' }
]
}
}
}
Vue.createApp(app).mount('#app')
</script>
Try it yourself »
Using v-for in the <template> element:
v-for
Try it yourself »
v-for Iterating Over Objects
v-for can iterate over data through an object's properties:
v-for
Try it yourself »
You can also provide the second parameter as the key name:
v-for
Try it yourself »
The third parameter is the index:
v-for
Try it yourself »
v-for Iterating Over Integers
v-for can also loop over integers
Displaying Filtered/Sorted Results
We can process the elements of an array before displaying them. Generally, we can create a computed property to return the filtered or sorted array.
v-for Example
Output the even numbers in the array:
Try it yourself »
Using v-for/v-if Together
The above example uses v-for/v-if together to set a default value for the select:
v-for/v-if Example
v-for loops out the list, v-if sets the selected value:
Try it yourself »
Using v-for on Components
If you haven't learned about components yet, you can skip this section.
On custom components, you can use v-for just like on any normal element:
<my-component v-for="item in items" :key="item.id"></my-component>
However, no data will be automatically passed to the component, because the component has its own independent scope. To pass the iterated data to the component, we need to use props:
<my-component v-for="(item, index) in items" :item="item" :index="index" :key="item.id" ></my-component>
The reason for not automatically injecting item into the component is that it would tightly couple the component to the operation of v-for. Making the source of component data explicit allows the component to be reused in other situations.
Below is a complete example of a simple todo list:
Example
Try it yourself »