此组件的开发是为了后台管理的多图上传,并实现本地预览修改,当用户确认后开始整体提交,此组件使用了我的另一篇文章所用到的组件

所遇到的问题?

选择多个文件后无法实现本地预览?

  • 使用es6的for of 对获取到的文件对象数组 filelist进行迭代循环
  • 使用 FileReader 对象将选择的图片文件转换成可以前端展示的 base 64数据
  • 在新数组被将图片数据push进去就可以实现本地多图预览了
  1. fileChange(files) { // files 为接收到的文件对象
  2. const _this = this // 保存this
  3. for (let i = 0; i < files.length; i++) { //对文件数组对象进行迭代循环
  4. const reader = new FileReader()
  5. this.imgfiles.push(files[i])
  6. reader.readAsDataURL(files[i])
  7. reader.onloadend = function() {
  8. console.log(this.result)
  9. _this.imgs.push(this.result)
  10. }
  11. }

对不要的图片进行删除?

  • 获取图片数组下标,使用数组 slice方法进行删除
    获取下标:
  1. <div class="img-box" v-for="(item, i) in imgs" :key='i' >
  2. <img :src="item" alt=""> <span @click="del(i)"><i class="el-icon-delete"></i></span>
  3. </div>

v-for 的i就代表了数组的下标,因为我们不仅需要删除图片数组,还需要删除文件数组,删除操作:

  1. del(i) {
  2. this.imgfiles.splice(i, 1)
  3. this.imgs.splice(i, 1)
  4. this.imgsChange()
  5. },

将获取到的文件对象向父组件进行广播

  1. imgsChange() {
  2. this.$emit('imgsChange', this.imgfiles) //广播事件名:imgsChange,参数 this.imgfiles
  3. }

下面是我封装的组件的完整代码,没有进行更多的操作,仅仅是把选择的图片可以本地预览,删除,增加

  1. <template>
  2. <div class="img-files flex">
  3. <div v-if='imgs.length>0' class="flex">
  4. <div class="img-box" v-for="(item, i) in imgs" :key='i' >
  5. <img :src="item" alt=""> <span @click="del(i)"><i class="el-icon-delete"></i></span>
  6. </div>
  7. </div>
  8. <div class="img-file" v-if= 'imgs.length<size'>
  9. <img-upload @fileChange='fileChange' multiple='true'> <i class="el-icon-plus avatar-uploader-icon"></i></img-upload>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. import ImgUpload from '@/components/ImgUpload'
  15. export default {
  16. name: 'imgFiles',
  17. props: {
  18. size: {
  19. type: Number,
  20. default: 3
  21. }
  22. },
  23. data() {
  24. return {
  25. imgs: [],
  26. imgfiles: []
  27. }
  28. },
  29. components: {
  30. ImgUpload
  31. },
  32. methods: {
  33. fileChange(files) {
  34. console.log(files)
  35. const _this = this
  36. for (let i = 0; i < files.length; i++) {
  37. const reader = new FileReader()
  38. this.imgfiles.push(files[i])
  39. reader.readAsDataURL(files[i])
  40. reader.onloadend = function() {
  41. console.log(this.result)
  42. _this.imgs.push(this.result)
  43. }
  44. }
  45. this.imgsChange()
  46. },
  47. del(i) {
  48. this.imgfiles.splice(i, 1)
  49. this.imgs.splice(i, 1)
  50. this.imgsChange()
  51. },
  52. imgsChange() {
  53. this.$emit('imgsChange', this.imgfiles)
  54. }
  55. }
  56. }
  57. </script>
  58. <style scoped lang="scss" rel="stylesheet/scss">
  59. .img-box{
  60. position: relative;
  61. border:1px solid #e6e6e6;
  62. margin-right: 20px;
  63. padding-top:30px;
  64. background: #f6f6f6;
  65. img{
  66. width: 160px;
  67. }
  68. span {
  69. position: absolute;
  70. top:5px;
  71. right: 10px;
  72. color: red;
  73. }
  74. }
  75. .img-file{
  76. height: 190px;
  77. width: 190px!important;
  78. display: flex;
  79. align-items: center;
  80. font-size: 30px;
  81. text-align: center;
  82. border: 1px #e6e6e6 dashed;
  83. justify-content: center;
  84. margin: 0;
  85. }
  86. </style>

版权声明:本文为yuanchenchun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yuanchenchun/p/9505125.html