Vue.js Loop Statements
In Vue, loop statements are mainly implemented through the v-for directive, used to iterate over arrays or objects and generate corresponding numbers of elements.
Use the v-for directive on an element to loop-render elements based on the source data's array or object.
Iterate over an array:
v-for="(item, index) in items"
Iterate over an object:
v-for="(value, key, index) in object"
-
The Role of key: Using
v-forto render a list, you must provide a uniquekeyattribute for each item, so Vue can identify each item's uniqueness and perform efficient DOM updates. -
Nested Loops: You can nest multiple
v-forto render multi-dimensional arrays or object structures.
v-for can bind data to an array to render a list:
v-for Example
-
{{ site.text }}
Try it »
v-for also supports an optional second argument, whose value is the index of the current item:
v-for Example
index is the index value of the list item:
-
{{ index }} -{{ site.text }}
Try it »
Template
using v-for in:
< ul site in sites " < li {{ site.text }}
li Try it » v-for can iterate data through an object's properties: value in object " {{ value }}
li
ul
div < script const app = { data() { return { object: { name: 'Runoob', url: 'http://www.runoob.com', slogan: 'Learn not just technology, but dreams!' } } } } Vue.createApp(app).mount('#app')
script Try it » You can also provide a second argument as the key name: (value, key) in object " {{ key }} : {{ value }}
li
ul
div Try it » The third argument is the index: (value, key, index) in object " {{ index }}. {{ key }} : {{ value }}
li
ul
div Try it » v-for can also loop over integers n in 10 " {{ n }}
li
ul
div Try it » We can process the array's elements before displaying them; usually we create a computed property that returns the filtered or sorted array. Output the even numbers in the array: n in evenNumbers " {{ n }}
li
ul
div Try it » The example above uses v-for/v-if together to set a default value for a select: v-for loops out the list, and v-if sets the selected value: (site,index) in sites " :site = " site " :index = " index " :key = " site.id " index == 1 " :value = " site.name " selected {{site.name}}
option You selected: {{selOption}}
div
div < script const app = { data() { return { selOption: "Runoob", sites: [ {id:1,name:"Google"}, {id:2,name:"Runoob"}, {id:3,name:"Taobao"}, ] } }, methods:{ changeVal:function(event){ this.selOption = event.target.value; alert("You selected "+this.selOption); } } } Vue.createApp(app).mount('#app')
script Try it » If you have not yet learned about components, you can skip this section. On a custom component, you can use v-for just like on any ordinary element: However, no data is automatically passed into the component, because the component has its own independent scope. To pass the iterated data into the component, we use props: The reason for not automatically injecting item into the component is that it would tightly couple the component to v-for's operation. Making the component's data source explicit allows the component to be reused in other contexts. Here is a complete example of a simple todo list: . prevent = " addNewTodo " Add a todo
label E.g. Feed the cat " /> (todo, index) in todos " :key = " todo.id " :title = " todo.title " @ remove = " todos.splice(index, 1) "
todo-item
ul
div Try it »v-for
<
template
v-for
=
"<
li
li
template
ulv-for Iterating Objects
v-for
<div id="app">
<ul>
<li v-for=">v-for
<div id="app">
<ul>
<li v-for=">v-for
<div id="app">
<ul>
<li v-for=">v-for Iterating Integers
v-for
<div id="app">
<ul>
<li v-for=">Display Filtered/Sorted Results
v-for Example
<div id="app">
<ul>
<li v-for=">Using v-for/v-if Together
v-for/v-if Example
<
div
id
=
"
app
"
<
select
@
change
=
"
changeVal($event)
"
v-model
=
"
selOption
"
<
template
v-for
=
"<option v-if="><
option
v-else
:value
=
"
site.name
"
{{site.name}}
option
template
select
<
divUsing v-for on Components
Example
<div id="todo-list-example">
<form v-on:submit><label for="new-todo"><input v-model="newTodoText" id="new-todo" placeholder="><
button
Add
button
form
<
ul
<
todo-item
v-for
=
"