小程序首页onLaunch,onLoad为异步,调用app.js中的全局参数的解决方案。

发布于 5 年前作者 lujun4156 次浏览最后编辑 5 年前来自 share

解决方案就是在app.js加一个回调函数

   wx.login({
        failres => {
          console.log(res)
        },
        successres => {
          util.request(api.dl, {
            code: res.code,
          }, "GET").then(res => {
            if (this.employIdCallback){
              this.employIdCallback(res);
           }
            this.globalData.userid=res
            

然后在你的首页onLoad进行判断是否有这个值然后进行不同的逻辑

onLoad: function (options) {
   
    console.log( )
    if(!app.globalData.userid){
      this.getstore()
      this.getfl()
    }else{
      app.employIdCallback= param1=> {
        this.getstore()
        this.getfl()
   }
    }
   
   
  },

1 回复
kangxiulan
kangxiulan1 楼10 个月前

试试promise你会打开新世界大门