一:初始化显示指定界面一般对于后台功能比如商家、管理员等需要先登录才能进入到app所以需要在打开的时候来判断用户是否登录seo布局策略所以需要在打开的时候来判断用户是否登录从而决定是进入app还是登录界面。那么在小程序中我 ...
一:初始化显示指定界面
一般对于后台功能比如商家、管理员等需要先登录才能进入到app所以需要在打开的时候来判断用户是否登录从而决定是进入app还是登录界面。
那么在小程序中我们怎么来进行登录的判断呢?
大家都知道在小程序中我们注册页面是通过 app.json 这个文件 的pages字段。
{
"pages":[
"page/login/index",
"page/index/index",
]
}
注册之后打开小程序会自动显示注册在最前面的页面这里也就是 page/login/index
你会发现就算你登录之后也还是会进入到登录界面但是我们需要在用户登录之后跳转到page/index/index所以这里我们需要加逻辑判断来切换跳转
由于注册入口是app.json而非js文件所以这里不能加条件判断看来不能从这里下手
对于单入口程序来说一般都是在入口文件进行判断看文档我们会发现小程序的入口文是app.js并有对应的生命周期
我们或许可以在onLaunch做处理
App({
onLaunch:function(){
let user =UserModel.getUserSync();
if(user){
wx.redirectTo({url:'page/index/index'});
return
}
}
});
上面逻辑就是如果用户登录跳转到首页如果首页是tabbar中的请使用wx.switchTab方法看上去很完美。
运行测试一下
WAService.js:3 jsEnginScriptError
Cannot read property'webviewId' of undefined
TypeError:Cannot read property'webviewId' of undefined
at x (http://700744025.appservice.open.weixin.qq.com/WAService.js:5:26872)
at .<anonymous>(http://700744025.appservice.open.weixin.qq.com/WAService.js:5:28821)
at http://700744025.appservice.open.weixin.qq.com/WAService.js:6:688
at http://700744025.appservice.open.weixin.qq.com/WAService.js:4:2530
at Array.forEach (native)
at .<anonymous>(http://700744025.appservice.open.weixin.qq.com/WAService.js:4:2510)
at http://700744025.appservice.open.weixin.qq.com/WAService.js:4:11420
at n.<anonymous>(http://700744025.appservice.open.weixin.qq.com/asdebug.js:1:11421)
at n.emit (http://700744025.appservice.open.weixin.qq.com/asdebug.js:1:7932)
at r (http://700744025.appservice.open.weixin.qq.com/asdebug.js:1:1470)