小程序
小游戏
企业微信
微信支付
扫描小程序码分享
第一次进入小程序的时候,执行
wx.setStorageSync('key', 'value')
然后打开一个新页面,执行
Page({
data: {
userData: wx.getStorageSync(
'key'
),
},
获取出来的值为空的
8 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
我在page外面调用,获取也是空的
const userData = wx.getStorageSync('userData');
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
Page 这个方法在页面被跳转前就已经执行了,那个时候 getStorage 发生在 set 之前。
建议在 onLoad 中调用 getStorage
这是不是小程序的bug啊,给个回复
获取了登录信息后写入本地缓存,然后在页面手动点击跳转到新页面
是在 getUserInfo 以后才跳转到新页面的吗?
第一次进小程序会有问题,第二次以后就没问题了
//index.js
//获取应用实例
const
app = getApp()
motto:
'Hello World'
,
userInfo: {},
hasUserInfo:
false
canIUse: wx.canIUse(
'button.open-type.getUserInfo'
)
//事件处理函数
bindViewTap:
function
() {
wx.navigateTo({
url:
'../logs/logs'
})
onLoad:
if
(app.globalData.userInfo) {
this
.setData({
userInfo: app.globalData.userInfo,
true
}
else
(
.data.canIUse){
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
userInfo: res.userInfo,
{
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
getUserInfo:
(e) {
console.log(e);
app.globalData.userInfo = e.detail.userInfo;
wx.setStorageSync(
'e.detail.userInfo'
);
userInfo: e.detail.userInfo,
//logs.js
util = require(
'../../utils/util.js'
logs: [],
userInfo: wx.getStorageSync(
onLoad: function () {
//这里打印获取出来的是空的
console.log(
.data.userInfo);
logs: (wx.getStorageSync(
'logs'
) || []).map(log => {
return
util.formatTime(
new
Date
(log))
其他文件都是官方例子
请提供一下调用
前后的完整代码
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
我在page外面调用,获取也是空的
const userData = wx.getStorageSync('userData');
Page 这个方法在页面被跳转前就已经执行了,那个时候 getStorage 发生在 set 之前。
建议在 onLoad 中调用 getStorage
这是不是小程序的bug啊,给个回复
获取了登录信息后写入本地缓存,然后在页面手动点击跳转到新页面
是在 getUserInfo 以后才跳转到新页面的吗?
第一次进小程序会有问题,第二次以后就没问题了
//index.js
//获取应用实例
const
app = getApp()
Page({
data: {
motto:
'Hello World'
,
userInfo: {},
hasUserInfo:
false
,
canIUse: wx.canIUse(
'button.open-type.getUserInfo'
)
},
//事件处理函数
bindViewTap:
function
() {
wx.navigateTo({
url:
'../logs/logs'
})
},
onLoad:
function
() {
if
(app.globalData.userInfo) {
this
.setData({
userInfo: app.globalData.userInfo,
hasUserInfo:
true
})
}
else
if
(
this
.data.canIUse){
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this
.setData({
userInfo: res.userInfo,
hasUserInfo:
true
})
}
}
else
{
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this
.setData({
userInfo: res.userInfo,
hasUserInfo:
true
})
}
})
}
},
getUserInfo:
function
(e) {
console.log(e);
app.globalData.userInfo = e.detail.userInfo;
wx.setStorageSync(
'key'
,
'e.detail.userInfo'
);
this
.setData({
userInfo: e.detail.userInfo,
hasUserInfo:
true
})
}
})
//logs.js
const
util = require(
'../../utils/util.js'
)
Page({
data: {
logs: [],
userInfo: wx.getStorageSync(
'key'
),
},
onLoad:
function
() {
//这里打印获取出来的是空的
console.log(
this
.data.userInfo);
this
.setData({
logs: (wx.getStorageSync(
'logs'
) || []).map(log => {
return
util.formatTime(
new
Date
(log))
})
})
}
})
其他文件都是官方例子
请提供一下调用
前后的完整代码