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

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

I have a Vue component like bellow:

... data(){ items:
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 16:37

    Create an a method:

      methods: {
        roleCount(key) {
          return key + 1;
        },
      },
    

    If the array keys are are numbered, starting with zero. items[0], items[1], etc..
    you can use the array's keys

    {{ incementIndex(key) }}

    But if the array keys are typeof String then you can do

    {{ incementIndex(index) }}

    The second version uses the "counter" from v-for loop.

提交回复
热议问题