I have a Vue component like bellow:
<div v-for="item in items" :key="there I want get the for-loop index" >
</div>
...
data(){
items: [{name:'a'}, {name:'b'}...]
}
How can I get the index when I execute the for-loop in my vue.js?
v-for directive is a Vue. js directive used to loop over a data usually an array or object. First, we will create a div element with id as app and let's apply the v-for directive to an element with data.
The purpose of this key attribute is to give "a hint for Vue's virtual DOM algorithm to identify VNodes when diffing the new list of nodes against the old list" (from Vue. js Docs). Essentially, it helps Vue identify what's changed and what hasn't.
To get an element's height with Vue. js, we can assign a ref to the element we want to get the height for. Then we can get the height with the clientHeight property of the element that's been assigned the ref. We assigned the ref with the ref prop set to a name.
In the shorthand, everything before the argument (i.e. v-bind: ) is condensed into a single character, : . Here the argument is the event name to listen to: click . v-on has a corresponding shorthand, namely the @ character.
We get the index of each item from index. To get the v-for index in Vue.js, we can get it from the 2nd parameter.
Vue.js supports rendering lists of items onto the browser using the built-in v-for< core directive. In this post, we will explore how v-for can be used in Vue applications.
V-model in vue.js is defined to make a two-way binding process to speed up the development of web application by bundled with Vue.js. This v-model directive helps to bind a value to the component data also triggers the event whenever a user presses a key or paste option.
We can set Vue to track each element using a key. This would cause it to move elements rather than replacing values. This value should be unique to each element that’s being iterated over.
Declare an index
variable:
<div v-for="(item, index) in items" :key="item.name">
</div>
Demo:
new Vue({
el: '#app',
data: {
items: [{name: 'a'}, {name: 'b'}]
}
})
<script src="https://unpkg.com/vue"></script>
<div id="app">
<div v-for="(item, index) in items" :key="item.name">
{{ index }}: {{ item.name }}
</div>
</div>
Official docs section - Mapping an Array to Elements with v-for
(emphasis mine):
Inside
v-for
blocks we have full access to parent scope properties.v-for
also supports an optional second argument for the index of the current item.
Create a new method:
methods: {
incrementIndex(key) {
return key + 1;
},
},
If the array keys are numbered, starting with zero like items[0]
, items[1]
, etc..
, you can use the array's keys:
<div v-for="(item, key) in items" :key="key">
{{ incrementIndex(key) }}
</div>
But if the array's keys are typeof String
then you can do:
<div v-for="(item, key, index) in items" :key="key">
{{ incrementIndex(index) }}
</div>
The second version uses the counter
from the v-for
loop.
You can use `$index` to get the index of v-for.
<div v-for="item in items" :key="`$index`" >
</div>
and the other method:
<div v-for="(item, index) in items" :key="index" >
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With