2017-03-02 55 views
3

我在我的应用程序中集成了Google picker API。 我下面的官方文档Google Picker API谷歌拾取器API + Angular2获取未捕获TypeError:无法读取未定义的属性'handleAuthResult'

谷歌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){ 
} 

我刚开始g未捕获的TypeError:无法读取未定义的属性'handleAuthResult'。 谁能请帮助我..

回答

2

我觉得load方法没有给词法this回调里面,尝试绑定this给你的方法:

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

遇到错误“无法设置属性‘oauthToken’未定义在webpackJsonp.486.AppComponent.handleAuthResult“ –

+0

@ RamaKrishna不能说没有看到代码什么,可能会提出一个新的问题? – echonax

相关问题