在打字稿中,如何在窗口函数中访问变量?

时间:2020-06-17 22:37:44

标签: typescript

我想在Google Maps Api加载后向BehaviourSubject发出一个新值,但是我无法访问类参数,代码:

private isInitialized = new BehaviorSubject<boolean>(false);

  private initialization() {
    // Load Google Maps Api script
    var script = document.createElement('script');
    script.src = 'https://maps.googleapis.com/maps/api/js?key=' + environment.googlePlaces.apiKey + '&libraries=places&callback=initMap';
    script.async = true;

    window['initMap'] = function () { debugger; this.isInitialized.next(true); }
    document.head.appendChild(script);
  }

我输入了调试器,但是 this 与该类以外的其他东西相关联。

如何将 this 连接到类的实例?

1 个答案:

答案 0 :(得分:0)

替换:

window['initMap'] = function() { debugger; this.isInitialized.next(true); }

作者:

 window['initMap'] = () => { debugger; this.isInitialized.next(true); }