首页 > 其他分享 >uni-app H5在浏览器上刷新后回登陆页

uni-app H5在浏览器上刷新后回登陆页

时间:2023-01-17 11:58:00浏览次数:41  
标签:浏览器 配置文件 app H5 登陆 uni config

在uni-app 开发项目时,发见一个非常有意思的事情。本身是想 uni-app上加一个外部的配置文件,具体方法详见:

https://www.cnblogs.com/hailexuexi/p/16922509.html

当编译后,在www下运行是没有问题的,但如果用浏览器直接打开,偶尔就会出现读配置文件未定义的错误,可返回登陆页刷新几次偶尔就好用。

可是在www下运行就一点问题没有,困扰了很久一直没有找到根本的原因,最后只能用点浏览器后返回登陆页再是否已登陆,若已登陆过,再刷新一次,

来解决这个问题。下面记录下来解决方法。

index.vue 页中created()函数中判断如果 读配置文件 config.webSocketUrl 参数 出错 则回登陆页

created() {

            try{
                var url= config.webSocketUrl;
            }catch(e){
                console.log("读配置文件config.js 异常========" );
                uni.navigateTo({
                    url:"/pages/login",
                    animationType: "pop-in",
                    animationDuration: 300                    
                }) 
                return;
            }
            finally{
                console.log("--==1111 "+ config.webSocketUrl);
this.initWebsocket(); // created函数中调用
                this.tableDataList = tableData.data
                this.tableDataList2 = tableData2.data
            }
},

 

 

 login.vue页中的 onReady()函数中 判断   uni.getStorageSync('current_user_name')  是否为空,这个变量是登陆成功后赋的值

用它来判断是否已经登陆上了

onReady(){
     let userName = uni.getStorageSync('current_user_name');
     if(userName!=""){
         uni.clearStorageSync();//清除所有缓存数据
         location.reload();//刷新
     }          
},

目的就是 当读配置文件出错后,重回 登陆页 重新读取 配置文件

注:在 index.vue 中的退出按钮中 就不能调用  清除缓存数据 的函数了

logout(){                
    //uni.clearStorageSync();
    uni.navigateTo({
        url:"/pages/login",
        animationType: "pop-in",
        animationDuration: 300                    
    })
}

 

标签:浏览器,配置文件,app,H5,登陆,uni,config
From: https://www.cnblogs.com/hailexuexi/p/17057487.html

相关文章