以下是我的代码:

        var me = this;
        gapi.auth.authorize({ client_id: client, scope: scope, immediate: true }, function (authResult: any) {
            if (authResult && !authResult.error) {
                me.accessToken = authResult.access_token;

            } else {

              //TODO : show error in front end
            }
        });

当我像这样使用回调函数时。
gapi.auth.authorize({ client_id: client, scope: scope, immediate: true }, AuthResult);

function AuthResult(authResult: any) {
                if (authResult && !authResult.error) {
                    me.accessToken = authResult.access_token;

                } else {

                  //TODO : show error in front end
                }

我没有在回调函数中得到me属性
如何将回调函数包装到其他回调函数中,以便在js中也获得作用域

最佳答案

使用粗箭头:

    gapi.auth.authorize({ client_id: client, scope: scope, immediate: true },AuthResult);

    const AuthResult = (authResult: any) => {
            if (authResult && !authResult.error) {
                this.accessToken = authResult.access_token;

            } else {
              //TODO : show error in front end
            }

更多
不要使用.bindhttps://basarat.gitbooks.io/typescript/content/docs/tips/bind.html至少还没有

关于javascript - 如何在其他回调函数中包装回调函数并从那里调用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36321191/

10-13 04:48