gapi is not defined error comes when using with requirejs

亡梦爱人 提交于 2019-12-08 06:27:37

gp.js must be a module.

define(['jquery', 'googleplus'], function($, gapi) {
    # your code

    window.loginCallback = function (result) {
        if (result['status']['signed_in']) {
            gapi.client.load('plus', 'v1', function () {
                var request = gapi.client.plus.people.get(
                    {
                        'userId': 'me'
                    });
                request.execute(function (resp) {
                    var email = '';
                    if (resp['emails']) {
                        for (i = 0; i < resp['emails'].length; i++) {
                            if (resp['emails'][i]['type'] == 'account') {
                                email = resp['emails'][i]['value'];
                            }
                        }
                    }


                    var str = "Name:" + resp['displayName'] + "<br>";
                    str += "Email:" + email + "<br>";
                    str += "DOB:" + resp['birthday'] + "<br>";
                    str += "Gender:" + resp['gender'] + "<br>";
                    document.getElementById("profile").innerHTML = str;
                });
            });
        }
    };

    $('#login').click(login);
    $('#logout').click(logout);
});

And modify template:

<input type="button"  value="Login" id="login" />
<input type="button"  value="Logout" id="logout" />

Because gapi.signIn method require callback function in global namespace, loginCallback function must be global.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!