Vue.js loop statement
Loop using the v-for directive.
The v-for directive requires special syntax in the form site in sites , where sites is the source data array and site is an alias for iteration over the array elements.
v-for can bind data to an array to render a list:
v-for directive
<div id="app">
<ol>
<li v-for="site in sites">
{
{ site.name }}
</li>
</ol>
</div>
<script>
new Vue({
el: '#app',
data: {
sites: [
{ name: 'Runoob' },
{ name: 'Google' },
{ name: 'Taobao' }
]
}
})
</script>
Use v-for in the template:
v-for
<ul>
<template v-for="site in sites">
<li>{
{ site.name }}</li>
<li>--------------</li>
</template>
</ul>
v-for iterate object
v-for can iterate data through the properties of an object:
v-for
<div id="app">
<ul>
<li v-for="value in object">
{
{ value }}
</li>
</ul>
</div>
<script>
new Vue({
el: '#app',
data: {
object: {
name: '菜鸟教程',
url: 'http://www.kxdang.com/topic/',
slogan: '学的不仅是技术,更是梦想!'
}
}
})
</script>
You can also provide the second parameter as the key name:
v-for
<div id="app">
<ul>
<li v-for="(value, key) in object">
{
{ key }} : {
{ value }}
</li>
</ul>
</div>
The third parameter is the index:
v-for
<div id="app">
<ul>
<li v-for="(value, key, index) in object">
{
{ index }}. {
{ key }} : {
{ value }}
</li>
</ul>
</div>
v-for iterate over integers
v-for can also loop integers
v-for
<div id="app">
<ul>
<li v-for="n in 10">
{
{ n }}
</li>
</ul>
</div>