天天看點

小程式callback not a function

操作:

1. 如果是this.setData, 則更換this的指向,如:let _this = this
2. 在函數中通過參數回調時,會發現callback not a function, 則:
-- 将回調函數放到this下,在通過先加載回調函數,就可以了 ,如:
  // 選擇圖檔
  chooseImg() {
    let that = this;
    wx.chooseImage({
      success: function (res) {

        // 聲明回調函數和上傳選擇的圖檔
        that.uploadImgCallback= function (uploadImgArr) {
          let arrImg = that.data.tpArrayImg.concat(uploadImgArr)
          wx.setStorageSync('imgList', arrImg)
          wx.navigateTo({
            url: '/pages/editorPreview/editorPreview'
          })
        }
        that.uploadImg(res.tempFilePaths)

      },
    })
  },
  // 上傳圖檔
  uploadImg(imgArr) {

    if (imgArr.length == 0) {
      return;
    }

    let that = this
    let imgSrc = imgArr.shift()

    wx.uploadFile({
      url: app.globalData.host + '/album/saveImg',
      filePath: imgSrc,
      name: 'file',
      formData: {
        'album': wx.getStorageSync('setText') ? wx.getStorageSync('setText').title : 'default',
        'openid': wx.getStorageSync('openid')
      },
      success: res => {
        that.data.uploadImgSrc.push(res.data)
        if (imgArr.length > 0) {
          that.uploadImg(imgArr)
        } else {
          that.uploadImgCallback(that.data.uploadImgSrc)

        }
      }
    })
  },      

轉載于:https://www.cnblogs.com/maoriaty/p/9116285.html

繼續閱讀