【问题标题】:How can I call a function inside event with "this"如何使用“this”调用事件内部的函数
【发布时间】:2020-07-15 17:34:01
【问题描述】:

如何在这个事件中调用函数?

myfunction() {
  alert('bla bla!');
}
this.map.on('moveend', function() {
  console.log('moveend :: ' + this.getCenter());
  this.myfunction(); // => ERROR TypeError: this.myfunction is not a function
});

【问题讨论】:

    标签: javascript typescript ionic-framework leaflet ionic4


    【解决方案1】:

    试试下面的

    this.map.on('moveend', () => {
      console.log('moveend :: ' + this.getCenter());
      this.myfunction();
    });
    

    应该可以的。

    注意:您在这里遇到的问题是scope。我建议您阅读一下,这是一个很有价值的概念。

    【讨论】:

    • 如果 OP 需要更多关于“函数”和“箭头函数”之间区别的信息。我建议查看Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?
    • 感谢您的回复。我会遇到“this.getCenter()”错误 => 类型“HomePage”上不存在属性“getCenter”。 ://
    • 已解决(通过添加以下参数) this.map.on('moveend', ($event) => { console.log('moveend :: ' + $event.target. getCenter()); this.myfunction(); });非常感谢^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多