3
<form method="post" action="#" enctype="multipart/form-data">
<div class="row">
    <div class="col-md-12">
       <div class="image-upload" v-for="(item, index) in uploadVal" :key="item._id">
          <label class="btn btn-sm btn-success">
              <a-icon type="upload" />Upload
              <input
                type="file"
                ref="file"
                accept="image/*"
                @change="handleClickUpload"
                style="display: none;"
              />
        </label>
        <div class="show-img">
            <img :src="getImageURL(item.url)" />
        </div>
       </div>
    </div>
</div>
</form>

In methods:

methods: {
   handleClickUpload(e) {
      console.log(e.target.files);
   },
}

I am doing upload image in vuejs. And I want to get the index value of the v-for loop under the handleClickUpload of the methods. Is there any way to get index in handleClickUpload ? Please give me ideas.Thanks

deskeay
  • 263
  • 2
  • 15

2 Answers2

4

You can pass a function to it and pass the event and index externally as:

<input
  type="file"
  ref="file"
  accept="image/*"
  @change="(e) => handleClickUpload(e, index)"
  style="display: none"
/>

Live Demo

Codesandbox Demo

and get the index in the function handleClickUpload as:

handleClickUpload(e, i) {
  console.log(e.target.files);
  console.log(i);
},
DecPK
  • 24,537
  • 6
  • 26
  • 42
2

Try this:

@change="handleClickUpload($event, index)"
methods: {
  handleClickUpload(e, i) {
    console.log(e.target.files, i);
  },
}
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48