如何在组件外部点击触发组件内部的点击事件?
发布于 5 年前 作者 taogong 13029 次浏览 来自 问答
<image-cropper 
  id="image-cropper" 
  limit_move="{{true}}" 
  disable_rotate="{{true}}" 
  width="{{width}}" 
  height="{{height}}"
  imgSrc="{{src}}" 
  bindload="cropperload" 
  bindimageload="loadimage" 
  bindtapcut="clickcut"
  disable_ratio="{{disable_ratio}}"
></image-cropper>
<view class="submit"> 
<button class="subBtn" bindtap="submit">确定</button>
</view>

  clickcut(e) {
      //点击裁剪框阅览图片
      wx.previewImage({
          current: e.detail.url, // 当前显示图片的http链接
          urls: [e.detail.url] // 需要预览的图片http链接列表
      })
      console.log("url",e)
      wx.uploadFile({
        url'',
        filePath: e.detail.url,
        name'file',  //随意
          header: { 
            'Content-Type''multipart/form-data',
          },
          formData: {
            method'POST'   //请求方式
          },
          success(res) {
            console.log("success",res.msg)
          }
      })
  },
  submit(){
      this.clickcut()
  },

想要点击submit触发clickcut事件,但是直接在submit里面调用clickcut的话就没有参数,还是会调用失败

3 回复

你还是再看看插件的文档,可以使用插件的函数getImg获取url

类似这样:

clickcut(e) {
  this.fun(e.detail.url)
}

submit() {
   let that = this
  let cropper = this.selectComponent("#image-cropper");
  cropper.getImg(function(res){
    that.fun(res.url)
  })
}

fun(url) {
  wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
  })
  console.log("url", url)
  wx.uploadFile({
    url: '',
    filePath: url,
    name: 'file'//随意
      header: { 
        'Content-Type': 'multipart/form-data',
      },
      formData: {
        method: 'POST'   //请求方式
      },
      success(res) {
        console.log("success",res.msg)
      }
  })
}

搞个代码片段吧

你把submit的e传递进去。

回到顶部