FrontendDeveloper.in

Vue.js question detail

What is the purpose of v-for directive?

The built-in v-for directive allows us to loop through items in an array or object. You can iterate on each element in the array or object.

  1. Array usage:
<ul id="list">
<li v-for="(item, index) in items">
{{ index }} - {{ item.message }}
</li>
</ul>

var vm = new Vue({
el: '#list',
data: {
items: [
{ message: 'John' },
{ message: 'Locke' }
]
}
})

You can also use of as the delimiter instead of in, similar to javascript iterators.

  1. Object usage:
{{ index }}. {{ key }}: {{ value }}

var vm = new Vue({
el: '#object',
data: {
user: {
firstName: 'John',
lastName: 'Locke',
age: 30
}
}
})
Back to all Vue.js questions
Get LinkedIn Premium at Rs 399