taro转H5端踩坑

项目场景:

在利用taro进行多端开发时踩坑随记:


问题描述

在编译h5端的时候提示:
Uncaught TypeError: (prevProps.className || prevProps.class || "").split is not a function"

javascript 复制代码
return <ScrollView scrollY onScrollToLower={this.onScrollToLower} onRefresherRefresh={this.onRefresherRefresh} 
refresherTriggered={this.props.refresherTriggered} refresherEnabled={this.props.refresherEnabled} 
className={['app-tab-content',this.props.tabValue === this.props.name ? 'app-tab-content-show' : null]}>
{this.props.children}</ScrollView>

原因分析:

类名里出现了空字符串


解决方案:

javascript 复制代码
  return <ScrollView scrollY onScrollToLower={this.onScrollToLower} onRefresherRefresh={this.onRefresherRefresh}
        refresherTriggered={this.props.refresherTriggered} refresherEnabled={this.props.refresherEnabled}
         className={`app-tab-content ${this.props.tabValue === this.props.name && 'app-tab-content-show'}`}>{this.props.children}</ScrollView>

问题描述

scrollview标签在h5端不能滑动,但是刷新页面就能滑动了


原因分析:

ScrollView标签上设置了overflow:hidden属性


解决方案:

去掉overflow:hidden属性

问题描述

提示:这里描述项目中遇到的问题:

小程序调用Taro.chooseImage和H5端不同,h5端上传给后端报错


原因分析:

利用Taro.chooseImage上传图片之后上传给后端小程序和h5端返回的tempFilePaths不同

小程序:

h5端:


解决方案:

在小程序和h5的时候采用不同的方法传:

javascript 复制代码
  const handleUploadImage = () => {
        Taro.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success: async (res) => {
                const tempFilePath = res.tempFilePaths[0];
                uploadImageToServer(tempFilePath,res.tempFiles[0])
                let urlList = [...list]
                urlList.push(tempFilePath)
                setList(urlList)
            }
        });
    };
      const uploadImageToServer = async (filePath) => {
        let token = Taro.getStorageSync("token");
        const res = await Taro.uploadFile({
            url: api.xxxx,
            filePath:filePath,
            name: 'file',
            formData: {},
            header: {
                'X-Authorization': `Bearer ${token}`,
                'Content-Type': 'multipart/form-data', // 如果上传文件需要特定的 Content-Type,可以在这里设置
            },
        });
        if(process.env.TARO_ENV==="weapp"){
          const res = await Taro.uploadFile({
              url: api.xxx,
              filePath:filePath,
              name: 'file',
              formData: {},
              header: {
                  'X-Authorization': `Bearer ${token}`,
                  'Content-Type': 'multipart/form-data', // 如果上传文件需要特定的 Content-Type,可以在这里设置
              },
          });
          let parse = JSON.parse(res.data)
          setKeyKList([...keyKList, parse.data])
        }else if(process.env.TARO_ENV==="h5"){
          // h5返回得地址转换为blob
          fetch(filePath).then(fetchRes => {
          		return fetchRes.blob();
          	}).then(async data => {
             //blob格式转换为base64格式
          	blobToDataURI(data,async function ( result) {
              let res = await request.post({
                url: api.uploadBase,
                data: {
                  base64Str:result,
                  fileExt:baseFile.originalFileObj.name
                }
              });
               let parse = JSON.parse(res.data)
               setKeyKList([...keyKList, parse.data])
          	});
          })
        }
    };
    const blobToDataURI=(blob, callback) =>{
      let reader = new FileReader();
      reader.readAsDataURL(blob);
      reader.onload = function (e) {
      callback(e.target.result)
      }
    }
    

附上几种图片格式转换(File、Blob、base64)方法

相关推荐
java1234_小锋5 小时前
【技术专题】HTML5 网页技术 - HTML5 Canvas 基础以及动画入门
前端·html·html5
知了学历孙老师9 小时前
小自考的校考和统考,出题方和难度差在哪
java·大数据·前端
ShineWinsu10 小时前
对于JavaScript:操作元素的解析
开发语言·前端·javascript·面试·web·dom·元素
福兮说11 小时前
JSON 格式正确,数据就可靠吗?我用一份自编配置检查类型与空值
java·前端·json
程序员老赵13 小时前
Docker 部署青龙面板:轻松搭建定时任务管理平台
前端·docker·开源
Swot13 小时前
Asciidoctor 公式数字下标错位问题:原因分析与解决方案
前端
Swot13 小时前
leo-editor 中将 Markdown 内容转成 asciidoc 格式
前端
狒狒AI13 小时前
Remotion / GSAP+Three / HyperFrames / Skia 四栈实测:代码渲染知识视频的选栈方法(附开源技能)
前端·人工智能·aigc
修炼的dance13 小时前
浏览器里导出H.265,8.5秒和55.5秒差在硬件
前端
补码缩写13 小时前
WebCodecs选编码器,探测之后还要验解码和播放
前端