頁面加載后仍未獲取到openId和userInfo問題解決
標簽:
JavaScript
记得上次接小程序的项目已经是去年10月份了,隔了大半年,再次捡起来,这感觉,跟当初刚刚写的时候差不多。
先放个官方链接:https://developers.weixin.qq.com/miniprogram/dev/
参考文章
微信小程序实现watch属性监听数据变化 https://blog.csdn.net/xuyangxinlei/article/details/81408200
1.页面加载后仍未获取到openId
原因:app.js里的onLaunch(异步)方法调用得到数据的速度比页面Page中的onLoad慢,导致在加载index.wxml时openid总是为空
解决办法:采用Promise
app.js
App({ onLaunch: function () { let that = this; // 展示本地存储能力 var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) if (wx.getStorageSync('openid')){ this.getOpenid() } // 获取用户信息 wx.getSetting({ ...... }) },// getOpenid getOpenid: function () { var that = this; return new Promise(function (resolve, reject) { wx.login({ success: function (res) { //code 获取用户信息的凭证 if (res.code) { //请求获取用户openid wx.request({ url: "", //请求接口 data: { "code": res.code }, method: 'GET', header: { 'Content-type': 'application/json' }, success: function (res) { wx.setStorageSync('openid', res.data.data.openid);//存储openid var res = { status: 200, data: res.data.openid } resolve(res); } }); } else { console.log('失败' + res) } } }) }); }, })
index.wxml的onLoad方法
//获取openid app.getOpenid().then(function (res) { if (res.status == 200) { that.setData({ openId: wx.getStorageSync('openid') }) } else { console.log(res.data); } });
2.userInfo获取慢
原因:跟上一个问题差不多
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦