uniapp开发中的踩坑集合

4次阅读

共计 1502 个字符,预计需要花费 4 分钟才能阅读完成。

uni-app 官网指路

uni-app 开发中的坑

1. 无法覆盖 uni-app 提供的组件的样式

直接重写样式会发现并不生效

 编译到 H5 如果要重置组件样式使用
    >>> .className{width:xxx}

scss 的话 要使用 
    /deep/ .uni-radio-input {
        width: 32upx;
        height: 32upx;
        background: #fff !important;
        border: 2upx solid #CCCCCC !important;
    }

2.ios 真机下键盘遮挡住弹框底部部分内容

rt

解决方案

在 pages.json 中配置

    {
    "path": "pages/normative-interpretation/normative-interpretation-info/normative-interpretation-info",
    "style": {
        "app-plus":{"softinputMode": "adjustResize"}
        }
    },

3.uni-app 文件上传 & 多文件上传

官网文档指路:https://uniapp.dcloud.io/api/request/network-file

选择文件:

    uni.chooseImage({
        count: 3,
        async success(e) {const res = await handleFileButhUpload(e.tempFilePaths)
        if (res) {
            const imgUrls = res.data
            that.imgUrls = imgUrls
        }
    },
        fail(err) {
            uni.showToast({
            icon: 'none',
            title: '图片选择失败,请稍后重试'
            })
        }
    })

单文件 (可附带传其他参数)

export const handleUserAvatarUpload = (id, filePath) => {return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: BASE_URL + '/community/xfFile/uploadHeadimage',
            filePath: filePath, // uni.chooseImage 函数调用后获取的本地文件路劲
            name: 'file',
            formData: {id},
            success: (res) => {resolve(res)
            },
            fail: (err) => {reject(err)
            }
        });
    })
}

多文件:
我这边后台接口是要我传 files 的文件对象, 这东西折腾我大半天,在传参的时候碰到过传的 file 为 {},后来我这边对选中的文件做了处理。

// 处理选中的文件
export const getFilecalculate = (data) => {const newData = [];
    data.forEach((item, index) => {
        newData.push({
            name: 'files',
            uri: item
        })
    })
    return newData
}

调用官方 api

export const handleFileButhUpload = (filePaths) => {const files = getFilecalculate(filePaths)
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            url: BASE_URL + '/community/xfFile/uploadImage',
            files,
            success: (res) => {const newRes = JSON.parse(res.data)
                resolve(newRes)
            },
            fail: (err) => {reject(err)
            }
        });
    })
}
正文完
 0