【发布时间】:2018-01-03 02:01:51
【问题描述】:
我正在我的应用程序中集成 Google 选择器 API。我在关注官方文档Google Picker API
我已经成功完成了我的工作,但是在添加以下代码后,我无法使用类方法和变量。获取无法读取未定义错误的属性
gapi.load('auth', {'callback': this.onAuthApiLoad.bind(a)});
完整代码为:
onApiLoad() {
var a= this;
gapi.load('auth', {'callback': this.onAuthApiLoad.bind(a)});
gapi.load('picker');
}
onAuthApiLoad() {
gapi.auth.authorize(
{
'client_id': this.clientId,
'scope': this.scope,
'immediate': false
},
this.handleAuthResult);
}
handleAuthResult(authResult) {
if (authResult && !authResult.error) {
if (authResult.access_token) {
var pickerBuilder = new google.picker.PickerBuilder();
var picker = pickerBuilder.
enableFeature(google.picker.Feature.NAV_HIDDEN).
setOAuthToken(authResult.access_token).
addView(google.picker.ViewId.DOCS).
setCallback(this.myFun).
build();
picker.setVisible(true);
}
}
}
myFun(e){
}
【问题讨论】:
-
this.handleAuthResult.bind(this)? -
谢谢极客,我是 javascript 新手,对这个 bind() 感到困惑,你能分享一下这方面的东西吗?
-
你在哪里得到错误?在
onAuthApiLoad的第一行放置一个断点。this在该断点处的值是多少? -
看看this post。
标签: javascript angular google-picker