gpt4 book ai didi

angular - Google Picker API + Angular2 获取未捕获类型错误 : Cannot read property 'handleAuthResult' of undefined

转载 作者:太空狗 更新时间:2023-10-29 18:31:42 26 4
gpt4 key购买 nike

我正在将 Google picker API 集成到我的应用程序中。我正在关注官方文档 Google Picker API

google api js 我按照文档的建议包含在我的 index.html 页面中

 <script type="text/javascript" src="https://apis.google.com/js/api.js"></script>

这是我在 html 页面中的 div :

<img src="../GoogleDrive.png(click)="onApiLoad()">  

这是我的组件代码:

  onApiLoad() {
let self = this;
gapi.load('auth', {'callback': self.onAuthApiLoad});
gapi.load('picker');
}

onAuthApiLoad() {
let self = this;
window.gapi.auth.authorize(
{
'client_id': "clientid",
'scope': ['https://www.googleapis.com/auth/drive.readonly'],
'immediate': false
},
self.handleAuthResult);
}

handleAuthResult(authResult:any) {
let self = this;
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
self.createPicker(oauthToken);
}
}

createPicker() {
let self = this;
if ( oauthToken) {
var pickerBuilder = new google.picker.PickerBuilder();
var picker = pickerBuilder
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.setOAuthToken(oauthToken)
.addView(google.picker.ViewId.DOCS_VIDEOS)
.setDeveloperKey('apikey')
.setCallback(self.pickerCallback)
.build();
picker.setVisible(true);
}
}

pickerCallback(data) {
let self = this;
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
self.downloadGDriveFile(doc.id , doc.name);
}
}

downloadGDriveFile(fileId, name){
}

我收到未捕获的类型错误:无法读取未定义的属性“handleAuthResult”。谁能帮帮我..

最佳答案

我认为 load 方法没有在回调中提供词法 this,尝试将 this 绑定(bind)到您的方法:

gapi.load('auth', {'callback': self.onAuthApiLoad.bind(this)});

关于angular - Google Picker API + Angular2 获取未捕获类型错误 : Cannot read property 'handleAuthResult' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42555523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com