How to get the v-for index in Vue.js?

前端 未结 4 1617
情歌与酒
情歌与酒 2021-01-07 16:18

I have a Vue component like bellow:

... data(){ items:
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-07 16:32

    Declare an index variable:

    Demo:

    new Vue({
      el: '#app',
      data: {
        items: [{name: 'a'}, {name: 'b'}]
      }
    })
    
    
    
    {{ index }}: {{ item.name }}

    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.

提交回复
热议问题