Home > Back-end >  Get value in v-for loop into methods in vuejs
Get value in v-for loop into methods in vuejs

Time:12-13

<form method="post" action="#" enctype="multipart/form-data">
<div >
    <div >
       <div  v-for="(item, index) in uploadVal" :key="item._id">
          <label >
              <a-icon type="upload" />Upload
              <input
                type="file"
                ref="file"
                accept="image/*"
                @change="handleClickUpload"
                style="display: none;"
              />
        </label>
        <div >
            <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

CodePudding user response:

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);
},

CodePudding user response:

Try this:

@change="handleClickUpload($event, index)"
methods: {
  handleClickUpload(e, i) {
    console.log(e.target.files, i);
  },
}
  • Related