声明和定义
// in app.jsglobalData:{userInfo:null,test:"test"}
跨页面调用
var app = getApp();//写在页面顶部page()外// in page.jsapp.globalData.test="123" //修改值直接使用“=”
example:
//app.jsApp({//全局变量globalData:{userInfo:null,sysInfo:null,windowW:null,windowH:null},//启动onLaunch: function () {// 获取用户信息this.getUserInfo();this.getSys();},//获取用户信息getUserInfo:function(cb){var that = thiswx.login({success: function () {wx.getUserInfo({success: function (res) {that.globalData.userInfo = res.userInfoconsole.log(res.userInfo);typeof cb == "function" && cb(that.globalData.userInfo)}})}})},//获取手机信息getSys:function() {var that = this;// 这里要非常注意,微信的scroll-view必须要设置高度才能监听滚动事件,所以,需要在页面的onLoad事件中给scroll-view的高度赋值wx.getSystemInfo({success: function(res) {console.log(res.model)console.log(res.pixelRatio)console.log(res.windowWidth)console.log(res.windowHeight)console.log(res.language)console.log(res.version)console.log(res.platform)//设置变量值that.globalData.sysInfo=res;that.globalData.windowW=res.windowWidth;that.globalData.windowH=res.windowHeight;}})}})
