天天看点

微信小程序app.js给全局变量globalData赋值

微信小程序app.js 中,从数据库读取登录数据后,想把登录信息变为全局遍历,这里为globalData赋值和其他的页面赋值不同。

需要先定义 that=this  之后用that.globalData进行赋值

onLaunch: function() {
    var that = this;
    wx.request({
      url: 'http://www.test.php/login',
      data: {
        UserCode: 'wechart',
      },
      method: 'post',
      success: function (res) {
        if (res.data.status == true){
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          }) 
          ID = res.data.data.ID;
          that.globalData.UserID = ID;
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'warn',
            duration: 2000
          }) 
        }
      }
    })