Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue.js Loop Statements

Vue.js Loop Statements

Use the v-for directive for looping.

The v-for directive requires a site in sites special 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: 'Runoob' }, { name: 'Google' }, { name: 'Taobao' } ] } })

script

Try it »

Using 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: 'Runoob Tutorial', url: 'http://www.runoob.com', slogan: 'Learning is not just about technology, but also about dreams!' } } })

script

Try it »

You can also provide a second argument 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 argument 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 »