zxyzb
zxyzb
  • 发布:2019-10-15 16:36
  • 更新:2022-05-19 14:02
  • 阅读:3567

uniapp H5端在异步回调中无法调用chooseImage

分类:uni-app
  1. uni.chooseImage 在手机浏览器中无法在异步回调中拉起;
  2. 可以在异步完成后调用uni.showModal 然后在showModal的success回调中再去调用uni.chooseImage可以拉起成功。
chooseImage: async function() {  
    let that = this;  
    if (this.imageList.length === 3) {  
        let isContinue = await this.isFullImg();  
        if (!isContinue) {  
            return;  
        }  
    }  

    uni.chooseImage({  
        sourceType: sourceType[that.sourceTypeIndex],  
        sizeType: sizeType[that.sizeTypeIndex],  
        count: that.imageList.length + that.count[that.countIndex] > 3 ? 3 - that.imageList.length : that.count[that.countIndex],  
        success: (res) => {  
            that.imageList = that.imageList.concat(res.tempFilePaths);  
        },  
        fail: (err) => {  
        }  
    })  
},  
isFullImg: function() {  
    let that = this;  
    return new Promise((res) => {  
        setTimeout(function() {  
            //如果我把uni.showModal这段代码注释掉替换成 res(true); ,uni.chooseImage 就不会在手机端的浏览器中调起来  
            uni.showModal({  
                content: "已经有3张图片了,是否清空现有图片?",  
                success: (e) => {  
                    if (e.confirm) {  
                        that.imageList = [];  
                        res(true);  
                    } else {  
                        res(false)  
                    }  
                },  
                fail: () => {  
                    res(false)  
                }  
            })  
        },2000)  
    })  
}  
0 关注 分享

要回复文章请先登录注册

jayZ

jayZ

博主这个办法实际上不太符合大多数需求,说说我的解决思路吧。我们可以给一个无关的dom节点绑定一个click方法,在异步回调的时候 获取节点触发click事件 这时候会发现pc浏览器已经可以成功调用,但是手机端还是无法成功调起。我们可以给这个click方法加一个 延时器 就可以在手机端成功调起了
2022-05-19 14:02
4***@qq.com

4***@qq.com

大佬啊,这个问题,不用showModal弹框该怎么优化呀,需求是直接弹出选择/(ㄒoㄒ)/~~
2021-11-29 12:05