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)方法

相关推荐
触底反弹1 小时前
🔥 React 零基础入门(下):Props + State + useEffect 生命周期深度解析
前端·react.js·面试
咖啡星人k1 小时前
GitHub Copilot 加入 Agent 浏览器:验证 Web 应用进入编码闭环
前端·人工智能·github·copilot·前端开发·ai agent
Shadow(⊙o⊙)1 小时前
高并发内存池:Part-1——定长内存池
java·前端·javascript
GuWenyue1 小时前
90%Node新手踩坑!path+fs两大内置模块完整实战,3种异步写法彻底告别回调地狱
前端
国服第二切图仔2 小时前
17-config命令 - 配置管理系统
java·前端·javascript
GuWenyue2 小时前
放弃云端API!5套技术栈实战WebGPU端侧AI,前端独立跑本地大模型,省成本还保隐私
前端·数据库·人工智能
胡萝卜术10 小时前
当大模型遇上浏览器:用 React + WebGPU 在前端跑通 DeepSeek-R1 的实战笔记
前端·javascript·面试
不好听61310 小时前
Tailwind CSS 原子化 CSS 完全入门:为什么现代前端开发都在用?
前端·css
触底反弹10 小时前
🔥 React 零基础入门(上):环境搭建 + JSX 深度解析
前端·react.js·typescript
why技术10 小时前
分享一套我一直在使用的 AICoding 组合拳,小而美的典范。
前端·后端·ai编程