uni-app reLaunch 跳转到启动页后,启动页的subNvue不会显示

uni-app reLaunch 跳转到启动页后,启动页的subNvue不会显示

开发环境 版本号 项目创建方式
Windows Windows 10 家庭中文版 HBuilderX
产品分类:uniapp/App

PC开发环境操作系统:Windows

HBuilderX类型:正式

HBuilderX版本号:3.2.3

手机系统:iOS

手机系统版本号:IOS 14

手机厂商:苹果

手机机型:iPhone11promax

页面类型:vue

打包方式:云端

App下载地址或H5网址:[https://wkzx.wang/templet/firstCommon?softId=71232&userId=3096](https://wkzx.wang/templet/firstCommon?softId=71232&userId=3096)

示例代码:


启动页:
```javascript
onLoad() {
    this.my_token = uni.getStorageSync("token");
    if(utils.isEmpty(this.my_token)){
        uni.hideLoading();
        uni.reLaunch({
            url: '/pages/login/login'
        })
    } else {
        this.url='../../hybrid/html/index.html';
        uni.showLoading({
            title:'加载中...',
        });
    }
},
onReady(){
    currentWebview = this.$scope.$getAppWebview();
    setTimeout(() => { 
        let subNVue = uni.getSubNVueById('title_area');
        subNVue.show('fade-in', 200, () => {  
    });

    let ctr_btnNvue=uni.getSubNVueById('ctr_btn');
    ctr_btnNvue.show('fade-in', 200, () => {  
    },1500);
}

登录页:

onLoad() {
    //模拟登录成功后跳转到首页
    setTimeout(() => { 
        uni.setStorageSync('token', '123456789');
        uni.reLaunch({
            url: '../index/index_aecc'
        });
    },5000)
},

pagejson的subNvue的配置:

"app-plus": {
    "subNVues": [
        {
            "id": "title_area",
            "path": "pages/index/subNVue/title_area",
            "style": {
                "position": "absolute",
                "top": "10px",
                "left": "15upx",
                "width": "720upx",
                "height": "25%",
                "background": "transparent"
            }
        },
        {
            "id":"ctr_btn",
            "path":"pages/index/subNVue/ctr_btn",
            "style": {
                "position": "absolute",
                "top": "20%",
                "left": "660upx",
                "width": "80upx",
                "height": "100%",
                "background": "transparent"
            }
        }
    ]
}

操作步骤: ios启动页通过reLaunch跳转到登录页后,登录页登陆成功后再通过reLaunch跳转至首页,此时subNvue不显示,Android无此问题

预期结果: ios启动页通过reLaunch跳转到登录页后,登录页登陆成功后再通过reLaunch跳转至首页,此时subNvue正常显示

实际结果: ios启动页通过reLaunch跳转到登录页后,登录页登陆成功后再通过reLaunch跳转至首页,此时subNvue不显示

bug描述: ios 在启动页面reLaunch到登录页后,登陆成功后再次relaunch到启动页时,在启动页的onReady中show subNvue ,subNvue不加载;Android无此问题;可以点击退出登录验证此问题,


更多关于uni-app reLaunch 跳转到启动页后,启动页的subNvue不会显示的实战教程也可以访问 https://www.itying.com/category-93-b0.html

9 回复

请提供一个完整的示例工程方便排查问题

更多关于uni-app reLaunch 跳转到启动页后,启动页的subNvue不会显示的实战教程也可以访问 https://www.itying.com/category-93-b0.html


好的,已上传代码!

解决了吗…

回复 曾小晨: 你如果遇到了请单独发帖反馈一下,上传一个可直接运行的示例工程吧

回复 DCloud_iOS_XHY: 我现在在排查,先排查了再说吧,不过遇到了一个其他的问题,在小米8 Android 10 的手机上,nvue 的渲染居然没有 vue 快,我现在只把首页弄成了 nvue,其他还是尽量 vue 吧,没有感觉到 nvue 的好处

回复 曾小晨: 正常情况是nvue渲染更快的,确认有问题可以单独发帖反馈一下

回复 DCloud_iOS_XHY: 但是没给工程示例,太麻烦了,有时间了再反馈吧

这是一个iOS平台特定的subNVue显示问题。主要原因是iOS上通过reLaunch重新进入页面时,subNVue的创建和显示时序与Android不同。

问题分析:

  1. iOS平台在reLaunch后,subNVue的初始化可能早于onReady执行
  2. 在onReady中获取subNVue时,iOS可能还未完全初始化完成
  3. 使用setTimeout延迟执行show操作,但时间点可能仍不准确

解决方案:

修改启动页的onReady方法,增加更可靠的检测机制:

onReady(){
    currentWebview = this.$scope.$getAppWebview();
    
    // 使用递归检测确保subNVue已完全初始化
    const showSubNVue = (retryCount = 0) => {
        if(retryCount > 10) return; // 最多重试10次
        
        let subNVue = uni.getSubNVueById('title_area');
        let ctr_btnNvue = uni.getSubNVueById('ctr_btn');
        
        if(subNVue && ctr_btnNvue) {
            subNVue.show('fade-in', 200);
            setTimeout(() => {
                ctr_btnNvue.show('fade-in', 200);
            }, 1500);
        } else {
            // 如果未获取到,延迟后重试
            setTimeout(() => {
                showSubNVue(retryCount + 1);
            }, 50);
        }
    }
    
    // 延迟执行,确保页面完全就绪
    setTimeout(() => {
        showSubNVue();
    }, 100);
}

替代方案: 如果上述方法仍不生效,可以考虑在页面onShow生命周期中处理subNVue显示:

onShow() {
    // 确保每次页面显示时都重新显示subNVue
    this.$nextTick(() => {
        setTimeout(() => {
            let subNVue = uni.getSubNVueById('title_area');
            let ctr_btnNvue = uni.getSubNVueById('ctr_btn');
            
            if(subNVue) subNVue.show('fade-in', 200);
            if(ctr_btnNvue) {
                setTimeout(() => {
                    ctr_btnNvue.show('fade-in', 200);
                }, 1500);
            }
        }, 300);
    });
}
回到顶部