【问题标题】:Google Picker API + Angular2 getting Uncaught TypeError: Cannot read property 'handleAuthResult' of undefinedGoogle Picker API + Angular2 获取未捕获的类型错误:无法读取未定义的属性“handleAuthResult”
【发布时间】:2017-03-02 12:26:57
【问题描述】:

我正在我的应用程序中集成 Google 选择器 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”。 谁能帮帮我..

【问题讨论】:

  • 我遇到了同样的问题....解决方案是什么?到“这个”不是“这个”

标签: angular angular2-routing google-picker


【解决方案1】:

我认为load 方法没有在回调中给出词法this,尝试将this 绑定到您的方法:

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

【讨论】:

  • 出现错误“无法在 webpackJsonp.486.AppComponent.handleAuthResult 设置未定义的属性 'oauthToken'”
  • @RamaKrishna 没有看到代码什么都说不出来,也许可以提出一个新问题?
猜你喜欢
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 2021-12-25
  • 2017-07-26
相关资源
最近更新 更多