uni-app TypeError: Cannot read property 'navigator' of undefined

uni-app TypeError: Cannot read property ‘navigator’ of undefined

项目 内容
PC开发环境操作系统 Mac
PC开发环境操作系统版本号 12.1
HBuilderX类型 正式
HBuilderX版本号 3.3.9
手机系统 Android
手机系统版本号 Android 10
手机厂商 华为
手机机型 mate20
页面类型 nvue
vue版本 vue2
打包方式 云端
项目创建方式 HBuilderX

示例代码:

afterCheckImUser: function(context) {
let res = { data: loginInfo['checkIm']['data'] };
if (res.data == 'noAccount') {
//没有账号,注册账号  == 'noAccount'
this.registerUser(loginInfo['ssId'].substr(0, 6), context, data => {
//注册成功
RBChatRestHelper.submitLoginToServer(
data.imAccount,
data.imPsd,
returnValue => {
/* console.log(JSON.parse(returnValue)); */
that.$store.dispatch('login', JSON.parse(returnValue))
uni.switchTab({
url: '/pages/tabbar/index/index'
})  

                },  
                returnValue => {  
                    this.errorMesg = returnValue || '服务器异常,稍后重试';  
                }  
            );  
        });  
    } else {  
        //有账号,通过查出来的账号密码登陆聊天室  
        let { imAccount, im$$ } = res.data;  
        RBChatRestHelper.submitLoginToServer(  
            imAccount,  
            im$$,  
            returnValue => {  
                /* console.log(JSON.parse(returnValue)); */  
                that.$store.dispatch('login', JSON.parse(returnValue))  
                uni.switchTab({  
                    url: '/pages/tabbar/index/index'  
                })  
            },  
            returnValue => {  
                console.error(returnValue || '服务器异常,稍后重试');  
            }  
        );  
    }  
},

更多关于uni-app TypeError: Cannot read property 'navigator' of undefined的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复

上传完整示例代码

更多关于uni-app TypeError: Cannot read property 'navigator' of undefined的实战教程也可以访问 https://www.itying.com/category-93-b0.html


问问题要贴完整代码,不然别人怎么给你解答

【社区问题/报bug正确姿势】:https://ask.dcloud.net.cn/article/38139

兄弟这个问题解决了吗

我也出现了,之前打包好好的

我的是加了微信端的判断忘了加编译条件了,报错没全部打出来,自己的锅 getPlatform: function() { let platform=config.getPlatform(); var ua = window.navigator.userAgent.toLowerCase(); //微信内浏览器 if(platform===‘H5’ && ua.match(/micromessenger/i) == ‘micromessenger’){ return ‘WX_GZH’; } return platform; },
仅供参考

该错误发生在nvue页面中,因nvue运行于原生渲染引擎,不包含浏览器navigator对象,而uni.switchTab的实现依赖该对象进行路由操作。在您的示例代码中,uni.switchTab被包含在异步回调内部,此时上下文中的uni对象可能尚未完全初始化或指向了错误的模块。

根本原因:nvue页面中,uni对象的某些API(尤其是涉及页面跳转的)需要通过plus或原生插件实现,直接调用会因缺失navigator属性而报错。

直接解决方案:在nvue页面中,改用uni.switchTab的替代方案——通过plus.webviewrequireNativePlugin('uni-tabbar')来切换tab。例如:

const tabBar = requireNativePlugin('uni-tabbar');
tabBar.switchTab({
  index: 0, // 目标tab的索引
  success: () => {}
});

或者使用:

plus.webview.hideAll();
const webview = plus.webview.getWebviewById('pages/tabbar/index/index');
plus.webview.show(webview);
回到顶部