Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display image by image uploaded on the vue component?

My code like this :

<div id="app">
  <div v-for="item in items">
    <div v-if="!image">
      <h2>Select an image</h2>
      <input type="file" @change="onFileChange">
    </div>
    <div v-else>
      <img :src="image" />
      <button @click="removeImage">Remove image</button>
    </div>
  </div>
</div>

Demo and full code like this : https://codepen.io/moschel26/pen/jwdMgp

There are 5 input files. I want when I upload the image on the input file 3 then the image is only shown on the img 3. when I upload the image on the input file 5 then the image is only shown on the img 5. etc

How can i do that?

like image 885
moses toh Avatar asked Nov 02 '25 00:11

moses toh


1 Answers

You should make array of objects to set uploaded images.

<div id="app">
  <div v-for="item in items">
    <div v-if="!item.image">
      <h2>Select an image</h2>
      <input type="file" @change="onFileChange(item, $event)">
    </div>
    <div v-else>
      <img :src="item.image" />
      <button @click="removeImage(item)">Remove image</button>
    </div>
  </div>
</div>

Full example here: https://codepen.io/emils/pen/VWgpaJ

like image 64
Emīls Gulbis Avatar answered Nov 04 '25 03:11

Emīls Gulbis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!