数组删除bug,严重
发布于 6 年前 作者 cyang 9579 次浏览 来自 问答

删除下标为0的需要删除两次才能删除

6 回复

.splice返回的是删除的数组值,重学JS吧

Component({

  /**

   * 组件的属性列表

   */

  properties: {

    maxNumber:{ // 最多上传文件数量

      type: Number,

      value: 1

    },

    uploadUrl: { // 上传接口url

      type: String,

      value: ‘’

    },

    uploadData: { // 上传时附带的其它参数

      type: Object,

      value: {}

    },

    fileName: { // 上传时文件对应的名称

      type: String,

      value: ‘file’

    }

  },

  /**

   * 组件的初始数据

   */

  data: {

    fileData: []

  },

  lifetimes: {

    attached: function() {

      // 在组件实例进入页面节点树时执行

 

    },

    detached: function() {

      // 在组件实例被从页面节点树移除时执行

    },

  },

  /**

   * 组件的方法列表

   */

  methods: {

    // 选择图片

    chooseFile: function(e){

      wx.chooseImage({

        count: this.maxNumber, // 上传文件数量(max:9)

        complete: function(res) {

          //this.upload(res.tempFilePaths, res.tempFiles);

 

          //  res.tempFiles :{path: ‘’, size: 11}

        }.bind(this),

      })

    },

    // 删除

    delete: function(e){

      let index = e.target.dataset.index;

      let data = this.data.fileData.splice(index, 1);  // <— 问题

      this.setData({

        fileData: data

      })

    },

    // 回调数据

    change: function(data){

      this.triggerEvent(‘change’, data, {})

    },

    upload: function(files = [], filesObject){

 

      this.data.fileData.push({url: files[0]});

      this.setData({

        fileData: this.data.fileData

      })

     }

  }

})

<view class=“upload-box”>

  <view class=“file-show-box” wx:for="{{fileData}}" wx:for-index=“i” bindtap=“showImg($event,i)” wx:for-key="{{i}}">

    <image class=“file-img” src="{{item.url}}"></image>

    <view class=“file-delete” bindtap=“delete” data-index="{{i}}">X</view>

  </view>

  <view class=“choose-file-btn”  bindtap=“chooseFile”>

    <text class=“choose-file-text”>最多{{maxNumber}}张</text>

  </view>

</view>

这么严重的bug都被你发现了,微信要倒闭了

整个代码片段

回到顶部