Vue.js Loop Statements

Loops use the v-for directive.

The v-for directive requires asite in sitesspecial syntax of the form, where sites is the source data array and site is the alias for the array element iteration.

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: 'Example' }, { name: 'Google' }, { name: 'Taobao' } ] } }) </script>

Try it »

Use v-for in a template:

v-for

<ul> <template v-for="site in sites"> <li>{{ site.name }}</li> <li>--------------</li> </template> </ul>

Try it »

v-for Iterating Objects

v-for can iterate data through an object's properties:

v-for

<div id="app"> <ul> <li v-for="value in object"> {{ value }} </li> </ul> </div> <script>new Vue({ el: '#app', data: { object: { name: 'Example Tutorial', url: 'http://www.example.com', slogan: 'What you learn is not just technology, but also dreams!' } } })</script>

Try it »

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>

Try it »

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>

Try it »

v-for Iterating Integers

v-for can also loop over integers

v-for

<div id="app"> <ul> <li v-for="n in 10"> {{ n }} </li> </ul> </div>

Try it »
Other Extensions