【问题标题】:method().bind(this) + Angular4 getting Cannot read property 'myFun' of undefined方法().bind(this)+ Angular4无法读取未定义的属性'myFun'
【发布时间】: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


【解决方案1】:

我已添加以下行来处理身份验证请求

this.handleAuthResult.bind(this)

【讨论】:

    猜你喜欢
    • 2018-01-16
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    相关资源
    最近更新 更多