app.js异步
发布于 7 年前 作者 xiaochao 4506 次浏览 来自 问答

上面是app.js中的onlaunch中的方法

上面是page  中的主页面

怎么解决这个app.js还没加载完 导致page中的getApp().globalData.userInfo为空的一步问题啊;有没有什么好的解决方案啊

1 回复

// app.js

getUserInfo: function(callback) {

    if (that.globalData.userInfo) {

        callback(that.globalData.userInfo);

        return;

    }

    wx.login({

        …

        success: function() {

            …

            wx.getUserInfo({

                …

                success: function(res) {

                    that.globalData.userInfo = res.userInfo;

                    callback(res.userInfo);

                }

            });

        }

    });

}

// page

onLoad: function() {

    getApp().getUserInfo(function(userInfo) {

        console.log(userInfo);

        …

});

}

回到顶部