uni-app 在 setTimeout 中延迟调用 uni.redirectTo 报错

uni-app 在 setTimeout 中延迟调用 uni.redirectTo 报错

开发环境 版本号 项目创建方式
Windows win10 HBuilderX

操作步骤:

  • 按照如上代码讲uni.redirectTo放在setTimeout中

预期结果:

  • 正常转跳

实际结果:

  • 正常转跳,但是控制台报错

bug描述:

基于uniCloudAdmin改造,leftWindow.vue中如下代码

clickMenuItem(url) {  
    // #ifdef H5  
    if (url.indexOf('http') === 0) {  
        return window.open(url)  
    }  
    // #endif  
    // TODO 后续要调整  

    if (this.toSave > 0) {  
        uni.showLoading({  
            title: "正在保存记录..."  
        })  
    }  
    let delay = this.toSave - new Date().getTime();  
    setTimeout(() => {  
        uni.redirectTo({  
            url: url,  
            success: () => {  
                // this.changeMenuActive(url)  
            },  
            fail: () => {  
                uni.showModal({  
                    title: '提示',  
                    content: '页面 ' + url + ' 跳转失败',  
                    showCancel: false  
                })  
            }  
        });  
        uni.hideLoading();  
    }, delay > 0 ? delay + 1000 : 0)  
},

运行后控制台报如下错误:

22:46:31.405 cid unmatched [object Object] at view.umd.min.js:1
22:46:31.426 TypeError: Invalid attempt to destructure non-iterable instance.
22:46:31.446 In order to be iterable, non-array objects must have a [Symbol.iterator]() method. at view.umd.min.js:1
22:46:31.466 cid unmatched [object Object] at view.umd.min.js:1
22:46:31.487 TypeError: Invalid attempt to destructure non-iterable instance.
22:46:31.508 In order to be iterable, non-array objects must have a [Symbol.iterator]() method. at view.umd.min.js:1
22:46:31.528 cid unmatched [object Object] at view.umd.min.js:1
22:46:31.549 TypeError: Invalid attempt to destructure non-iterable instance.
22:46:31.569 In order to be iterable, non-array objects must have a [Symbol.iterator]() method. at view.umd.min.js:1
22:46:31.590 cid unmatched [object Object] at view.umd.min.js:1
22:46:31.611 TypeError: Invalid attempt to destructure non-iterable instance.
22:46:31.631 In order to be iterable, non-array objects must have a [Symbol.iterator]() method. at view.umd.min.js:1
22:46:31.651 cid unmatched [object Object] at view.umd.min.js:1
22:46:31.672 TypeError: Invalid attempt to destructure non-iterable instance.
22:46:31.692 In order to be iterable, non-array objects must have a [Symbol.iterator]() method. at view.umd.min.js:1
22:46:31.712 cid unmatched [object Object] at view.umd.min.js:1
22:46:31.732 TypeError: Invalid attempt to destructure non-iterable instance.
22:46:31.753 In order to be iterable, non-array objects must have a [Symbol.iterator]() method. at view.umd.min.js:1
22:46:31.773 cid unmatched [object Object] at view.umd.min.js:1
22:46:31.793 TypeError: Invalid attempt to destructure non-iterable instance.
22:46:31.813 In order to be iterable, non-array objects must have a [Symbol.iterator]() method. at view.umd.min.js:1
22:46:31.834 cid unmatched [object Object] at view.umd.min.js:1
22:46:31.855 TypeError: Invalid attempt to destructure non-iterable instance.
22:46:31.875 In order to be iterable, non-array objects must have a [Symbol.iterator]() method. at view.umd.min.js:1
22:46:31.895 cid unmatched [object Object] at view.umd.min.js:1
22:46:31.916 TypeError: Invalid attempt to destructure non-iterable instance.
22:46:31.936 In order to be iterable, non-array objects must have a [Symbol.iterator]() method. at view.umd.min.js:1  

更多关于uni-app 在 setTimeout 中延迟调用 uni.redirectTo 报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

同问,

更多关于uni-app 在 setTimeout 中延迟调用 uni.redirectTo 报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个典型的异步调用问题。在uni-app中,页面跳转API(如redirectTo)如果在setTimeout等异步回调中调用,可能会因为上下文丢失导致报错。

解决方法:

  1. 使用uni-app的nextTick替代setTimeout:
uni.nextTick(() => {
    uni.redirectTo({
        url: url
    });
    uni.hideLoading();
});
  1. 如果必须使用延迟,建议将跳转逻辑放在methods中,保持this上下文:
methods: {
    doRedirect(url) {
        uni.redirectTo({
            url: url
        });
        uni.hideLoading();
    }
}

// 调用时
setTimeout(() => {
    this.doRedirect(url);
}, delay);
  1. 检查delay计算逻辑是否正确,确保delay是有效数值:
let delay = Math.max(0, this.toSave - Date.now() + 1000);
回到顶部