#2037·wepy

this.$nextTick(() => {})中的回调没有执行

Author: aben1188Created Jan 12, 2019Updated Sep 16, 2022

Description

调用this.$nextTick(() => {})之后,并没有执行传入的回调函数

Environment

  • Platform: 开发者工具
  • Platform version: v1.02.1812271
  • wepy version: 1.7.2

Reproduce

注:下面的代码来自该仓库:基于云开发制作名片识别小程序

             console.log('开始选择照片')
            // 从相册和相机中获取图片
            wx.chooseImage({
                success: dRes => {
                    wx.showLoading({
                        title: '照片上传中...',
                    })

                    let cloudPath = `${Date.now()}-${Math.floor(Math.random() * 1000)}.png`
                    console.log('cloudPath: ', cloudPath)

                    wx.cloud.uploadFile({
                        cloudPath: cloudPath,
                        filePath: dRes.tempFilePaths[0],
                        success: res => {
                            console.log('result of uploadFile: ', res)
                            if (res.statusCode < 300) {
                                /*this.setData({
                                    fileID: res.fileID,
                                }, () => {
                                     // 获取临时链接
                                    this.getTempFileURL()
                                })*/
                                this.fileID = res.fileID
                                //注:调用了$nextTick(),则不能再调用$apply(),
                                //否则将导致所传入的回调函数被立即执行,详见:
                                //https://github.com/Tencent/wepy/issues/1470
                                this.$nextTick(() => {
                                    console.log('test this.$nextTick()\'s callback')
                                    wx.hideLoading()
                                })
                            }
                        },
                        fail: err => {
                            wx.hideLoading()
                            wx.showToast({
                                title: '照片上传失败',
                                icon: 'none'
                            })
                        },
                    })
                },
                fail: err => console.error('选择照片失败:', err),
            })

Observed Results

在执行到this.$nextTick(() => {...})时,并没有执行传入的回调函数,控制台没有如期输出“test this.$nextTick()'s callback”,一直显示“照片上传中...”

Expected Results

执行传入this.$nextTick()中的回调函数,控制台正常输出“test this.$nextTick()'s callback”,关闭“照片上传中...” toast。