Vue: use $index in component

Created on 13 Sep 2016  路  5Comments  路  Source: vuejs/vue

Hi , i have a component <image-upload></image-upload> but i used it in v-for so i need to send the current index to component to use it when dispatch event , so how could i use something like <image-upload index="{{ $index }}"></image-upload> ?

Most helpful comment

First, define a prop called index inside <image-upload> that accepts a Number (you can give it another name if you'd like),
Then, pass the index using <image-upload :index="index"></image-upload>

Please read more about components and passing props here: http://vuejs.org/guide/components.html#Passing-Data-with-Props

All 5 comments

Hi, thanks for filling this issue, however the issue tracker is reserved for bug reports and feature requests only, so please use our dedicated gitter or forum to ask questions.


There is a section in the guide explaining this: http://vuejs.org/guide/list.html

<div v-for="(index, item) in items">
  {{ index }} {{ item.message }}
</div>

Please avoid using $index as it's deprecated in 2.0

@fnlctrl thanks for fast reply , i will post in the forum but may I ask you how to use it inside the component

<div v-for="(index, item) in items">
    <image-upload item-index="{{ index }}"></image-upload>
</div>

First, define a prop called index inside <image-upload> that accepts a Number (you can give it another name if you'd like),
Then, pass the index using <image-upload :index="index"></image-upload>

Please read more about components and passing props here: http://vuejs.org/guide/components.html#Passing-Data-with-Props

@fnlctrl WOW, thanks man!

<div v-for="(item, index) in items"></div>
<div v-for="(key, val) in object"></div>
<div v-for="(key, val, index) in object"></div>

see: http://rc.vuejs.org/api/#v-for

Was this page helpful?
0 / 5 - 0 ratings