以下是我的代码:
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
}
更多
不要使用 .bind
:https://basarat.gitbooks.io/typescript/content/docs/tips/bind.html至少现在还没有
关于javascript - 如何将回调函数包装在其他回调函数中并从那里调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36321191/