微信小程序的全局变量无法用
我在app.js中获取code把它作为全局变量,在index.js中获取code数据变空了
app.js
App({
onLaunch: function () {
wx.login({
//获取code
success: function (res) {
var code = res.code; //返回code
wx.code = res.code;
}
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
this.globalData.userInfo = res.userInfo
this.globalData.code = wx.code;
},
}
})
globalData: {
userInfo: null,
code:null
}
})
index.js
const app = getApp()
Page({
data: {
},
sendinfo: function () {
console.log("点击成功");
if(app.globalData.code){
console.log(app.globalData.code)
}else{
console.log(2)
}
}
})