![](https://box.kancloud.cn/551effc6483d6fc09a0db6fe98ece10c_560x323.png)
全局变量都是在app.js中定义,如上图所示
~~~
data:{
info:'',
mp_id:'d8d49a5800362843f29833e03038a72a',
},
onLaunch: function () {
// 当小程序初始化完成时,会触发 onLaunch(全局只触发一次)
// 调用API从本地缓存中获取数据
var that = this
this.store = new (jsonApi.JsonApiDataStore)
this.jsonModel = jsonApi.JsonApiDataStoreModel
this.globalData.code = wx.getStorageSync('code')![](https://box.kancloud.cn/083acb0ea4d97c349d67748a78fd5bc8_493x257.png)
this.getUserInfo()
},
~~~
全局变量的使用:
![](https://box.kancloud.cn/083acb0ea4d97c349d67748a78fd5bc8_493x257.png)
调取全局变量直接用getApp().data.变量名
~~~
var num = getApp().data.info.buyNumber
var sumPrice = getApp().data.info.price * num
~~~