van-uploader上传图片报错Invalid handler for event “load“(在uniapp编译)

van-uploader使用报错

原因:主要原因这里使用的vant版本是2.13.0的,在Hbuild里面运行的项目,vant插件在这里会有部分组件有兼容问题,(van-image,van-uploader等)。

解决 :主要是要实现图片上传功能,可以使用uniapp自带的上传组件(uni.uploadFile

官网地址

示例:

javascript 复制代码
<template>
 <scroll-view style="flex: 1">
   <view>
     <view class="uni-padding-wrap uni-common-mt">
       <view class="demo">
         <image
           v-if="imageSrc"
           :src="imageSrc"
           class="image"
           mode="widthFix"
         ></image>
         <text v-else class="uni-hello-addfile" @click="chooseImage"
           >+ 选择图片</text
         >
       </view>
     </view>
   </view>
 </scroll-view>
</template>
<script>
export default {
  data() {
    return {
      imageSrc: '',
      task: null,
    }
  },
  methods: {
    chooseImage: function () {
      uni.chooseImage({
        count: 1,
        success: (res) => {
          console.log('chooseImage success, temp path is', res.tempFilePaths[0])
          var imageSrc = res.tempFilePaths[0]
          uni.showLoading({
            title: '上传中'
          })
          this.task = uni.uploadFile({
            url: 'https://unidemo.dcloud.net.cn/upload', //仅为示例,非真实的接口地址
            filePath: imageSrc,
            name: 'file',
            formData: {
              'user': 'test'
            },
            //header: {
	        // "Content-Type": "multipart/form-data",
	        //Authorization: `${uni.getStorageSync('tokens')}`, // 请求携带token
	      //},
            success: (res) => {
              console.log('uploadImage success, res is:', res)
              uni.hideLoading();
              uni.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 1000
              })
              this.imageSrc = imageSrc
            },
            fail: (err) => {
              console.log('uploadImage fail', err);
              uni.hideLoading();
              uni.showModal({
                content: err.errMsg,
                showCancel: false
              });
            },
          });
        },
        fail: (err) => {
          console.log('chooseImage fail', err)
        }
      })
    },
  }
}
</script>

<style>
.image {
 width: 100%;
}

.demo {
 background: #fff;
 padding: 50rpx;
 justify-content: center;
 align-items: center;
}

.uni-hello-addfile {
 text-align: center;
 background: #fff;
 padding: 50rpx;
 margin-top: 10px;
 font-size: 38rpx;
 color: #808080;
}
</style>
相关推荐
程序员小刘2 小时前
Uniapp如何适配HarmonyOS5?条件编译指南以及常见的错误有哪些?
uni-app
你的人类朋友2 小时前
✍️【Node.js程序员】的数据库【索引优化】指南
前端·javascript·后端
CodeCraft Studio2 小时前
Excel处理控件Aspose.Cells教程:使用 C# 从 Excel 进行邮件合并
开发语言·c#·excel
小超爱编程3 小时前
纯前端做图片压缩
开发语言·前端·javascript
耶啵奶膘3 小时前
uniapp+vue2+h5图片下载保存,微信浏览器、非微信浏览器
uni-app·notepad++
islandzzzz4 小时前
HMTL+CSS+JS-新手小白循序渐进案例入门
前端·javascript·css·html
Senar4 小时前
网页中如何判断用户是否处于闲置状态
前端·javascript
很甜的西瓜4 小时前
typescript软渲染实现类似canvas的2d矢量图形引擎
前端·javascript·typescript·图形渲染·canvas
KIDAKN5 小时前
java--怎么定义枚举类
java·开发语言
海天胜景5 小时前
C# 中常用的 字符串截取方法
开发语言·c#