微信小程序改变全局变量

假设A为登录页面并将登录获得的用户信息保存到app.js中的全局变量userInfo中,然后在B页面进行使用。

 

app.js

 globalData:{
    userInfo:null,

}

 

a.js

 wx.request({
            …………

…………
        success: function (res) {
                  if (res.data.code == 0) {
                    app.globalData.userInfo=res.data.result;
                    
                    wx.redirectTo({
                      url: '.\b',
                    })

…………

…………

}

}

})

 

//在加载b页面时就将用户信息存到userInfo中

b.js

Page({
  data:{
    userInfo:[]
  },

onLoad:function(options){
      this.setData({
      userInfo:getApp().globalData.userInfo
    });
  },

})

posted @ 2018-03-25 16:14  慶哥  阅读(2545)  评论(0编辑  收藏  举报