简体   繁体   English

如何使用 Vue.Js 从数组中删除上传的多个图像,并且应该从 UI 中删除该图像?

[英]How to remove uploaded multiple images from array using Vue.Js and it should delete that image from UI?

I am uploading multiple images using VueJs.我正在使用 VueJs 上传多张图片。 Saving their respective base64 values in Array (to store them in db).将它们各自的 base64 值保存在 Array 中(将它们存储在 db 中)。 I have added remove feature there, while clicking on remove button I find the index of that element and splice it from the array.我在那里添加了删除功能,在单击删除按钮时,我找到了该元素的索引并将其从数组中拼接起来。

The clicked image value being removed from array thats fine but it does not change the position of the image.从数组中删除单击的图像值很好,但它不会改变图像的位置。 Visually I can see the last image is deleted.视觉上我可以看到最后一张图片被删除了。 How can I remove value of image from Array and remove same image from UI ?如何从 Array 中删除图像的值并从 UI 中删除相同的图像?

Here is the code: https://codepen.io/echobinod/pen/GVMOqJ这是代码: https : //codepen.io/echobinod/pen/GVMOqJ

<div id="app">
    <input type="file" multiple @change="onFileChange" /><br><br>
        <div class="row">
        <div v-for="(image, key) in images">
        <div class="col-md-4" :id="key">
        <button type="button" @click="removeImage(key)">
            &times;
        </button>
            <img class="preview img-thumbnail" v-bind:ref="'image' +parseInt( key )" /> 
            {{ image.name }}
        </div>
      </div>
    </div>
  </div>

<script>
const vm = new Vue({
  el: '#app',
  data() {
    return {
      images: [],
    }
  },
  methods: {
    onFileChange(e) {
      var selectedFiles = e.target.files;
      for (let i=0; i < selectedFiles.length; i++)
      {
        this.images.push(selectedFiles[i]);
      }
      for (let i=0; i<this.images.length; i++)
      {
            let reader = new FileReader(); //instantiate a new file reader
            reader.addEventListener('load', function(){
              this.$refs['image' + parseInt( i )][0].src = reader.result;
            }.bind(this), false);

        reader.readAsDataURL(this.images[i]);
       }
    },
    removeImage (i) { 
        var arrayImages = this.images;

        var index = arrayImages.indexOf(arrayImages[i]);

          arrayImages.splice(index, i);
    }
  }
})
</script>

You need to force the component to reload, updating the data does not mean that the UI will change. 您需要强制重新加载组件,更新数据并不意味着UI会更改。 to do this the best way imo is to put a key on whatever you need to reload, I think that would be here: 为了做到这一点,imo的最佳方法是在需要重新加载的内容上放置一个密钥,我想这就是这里了:

<div v-for="(image, key) in images" :key="reloadKey">
...
data(){
  return {
    reloadKey: 0}}
...
removeImage (i) { 
        var arrayImages = this.images;

        var index = arrayImages.indexOf(arrayImages[i]);

          arrayImages.splice(index, i);
this.reloadKey++
    }
  }

See here for further reading: https://michaelnthiessen.com/force-re-render 请参阅此处以进一步阅读: https : //michaelnthiessen.com/force-re-render

One of the good things about Vuejs is that it provides two way data binding between the dom and your data. Vuejs的优点之一是它在dom和您的数据之间提供了两种方式的数据绑定。 That means that when you change the images array defined in data the UI will automagically reflect the changes. 这意味着,当您更改data定义的images数组时,UI将自动反映更改。

As a quick fix, after filling the arrayImages array, assign this.images = arrayImages and the UI should update correspondingly. 作为快速解决方案,在填充arrayImages数组后,分配this.images = arrayImages ,UI应相应更新。

How about you use,你怎么用,

arrayImages.splice(index, 1);

Instead of using i in the splice而不是在拼接中使用 i

arrayImages.splice(index, i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM