(Micro-channel applet) the onload () less, than the value of global variable app.globalData

onLoad () to load less than the value of a global variable app.globalData

app.js

   globalData: {
       statusResult:null,
   }

index.js

	wx.request({
	   url: 'xxx',
	   method: 'POST',
	   header: {
	     'content-type': 'application/json',
	   },
	   success: function (res) {
	     var dataInfo = res.data.data;
	     var name = [];
	     for (let i in dataInfo) {
	       name  = dataInfo[i].Items.name
	     }
	     app.globalData.statusResult = name;  //把数据存到全局变量中
	   }
	 })

device.js

  data: {
    statusResult:[]
  },
  onLoad: function () {
    this.setData({
      statusResult: app.globalData.statusResult  
    })
  },

I finally print out the results, it is the first implementation decive.js in the onload (), acquired a global variable is null, then the request index.js then perform in.
I tried many methods, but finally added a loading and timers to solve this problem

  onLoad: function () {
      var that = this;
      wx.showLoading({
         title: '加载中',
      })
     setTimeout(function () {
        that.setData({
           statusResult : app.globalData.statusResult
        })
       wx.hideLoading()
     }, 1000)
  },

Released six original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43055905/article/details/82898902