解决微信小程序中的onLaunch执行比首页的onLoad晚一步的问题

解决微信小程序中的onLaunch执行比首页的onLoad晚一步的问题

放下链接,防止以后再次出错。

//app.js
App({
  onLaunch: function () {
    console.log('onLaunch');
    wx.request({
      url: 'test.php', //仅为示例,并非真实的接口地址
      data: {
      },
      success: function(res) {
        console.log('onLaunch-request-success');
        // 将employId赋值给全局变量,提供给页面做判断
        this.globalData.employId = res.employId;   
      }
    })
  },
  globalData: {
    employId: ''
  }
})
//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    bindDisabled: false
  },
  onLoad: function () {
    console.log('onLoad');
    console.log('onLoad app.globalData.employId = ' + app.globalData.employId);
    //判断是用户是否绑定了
    if (app.globalData.employId && app.globalData.employId != '') {
      this.setData({
        bindDisabled: true
      });
  }
})

原文链接