vue v-for loop array
<template>
<div>
<ul>
<li v-for="(item, index) in items" :key="index">
{{ item }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
items: ['Apple', 'Banana', 'Orange']
};
}
};
</script>
Explanation:
<template>
: Vue template tag.<div>
: Division container in the template.<ul>
: Unordered list HTML element.<li v-for="(item, index) in items" :key="index">
: Vue directivev-for
to iterate throughitems
array.(item, index)
destructuring syntax used to access each item and its index.:key="index"
assigns a unique key to each list item.{{ item }}
: Interpolation syntax to display the current item value.</li>
: Closing tag for list item.</ul>
: Closing tag for unordered list.</div>
: Closing tag for division container.<script>
: JavaScript script tag within Vue file.export default { ... }
: Exporting Vue component options.data() { ... }
: Vue data option to define reactive data.items: ['Apple', 'Banana', 'Orange']
: Initializingitems
array with strings 'Apple', 'Banana', and 'Orange'.