场景

由于微信小程序是异步加载的,所以基本上app.js的方法加载还没结束就page的方法就已经执行了

实现

使用promise实现回调函数

app.js
resolve用来回调请求成功的数据, reject用来回调请求错误的数据

checkDepositCompleteed(event) {
    // 检查基金模块用户是否已填写底金
    return new Promise((resolve, reject) => {
      const that = this
      depositModel.getUserDeposit().then(res => {
        const money = res.data['deposit']
        if (money > 0) {
          that.globalData.depositCompleteed = true
        } else {
          that.globalData.depositCompleteed = false
        }
        resolve(res)
      })
    })
  }

page js

onLoad: function (options) {
    app.checkUserLogined().then(res => {
      if (!res.data) {
        this.getOptionalData()
      } else {
        this.getOptionalData(res.data['token'])
      }
    })
  }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐