vue.jsHow do I use the v-for directive in Vue.js?
The v-for
directive is used in Vue.js to render a list of items based on an array. It works similarly to a for
loop in JavaScript.
Example code
<div id="app">
<ul>
<li v-for="item in items">{{ item.text }}</li>
</ul>
</div>
<script>
new Vue({
el: '#app',
data: {
items: [
{ text: 'Foo' },
{ text: 'Bar' },
{ text: 'Baz' }
]
}
})
</script>
Output example
Foo
Bar
Baz
The code consists of the following parts:
<div id="app">
: This is the root element for the Vue instance.<ul>
: This is the unordered list element which will contain the items.<li v-for="item in items">
: This is the directive which tells Vue to loop through theitems
array and create a list item for each item in the array.{{ item.text }}
: This is the text that will be displayed for each list item.
Helpful links
More of Vue.js
- How do I use the v-if directive in Vue.js?
- How to use a YAML editor with Vue.js?
- How can I implement pinch zoom functionality in a Vue.js project?
- How do I set a z-index in Vue.js?
- How can I use Yarn to install Vue.js?
- How do I use the v-model in Vue.js?
- How do I install Vue.js?
- How do I use Yup with Vue.js?
- How do I determine which version of Vue.js I am using?
- How do I create tabs using Vue.js?
See more codes...