如何使用 "this" 调用事件内部函数
How can I call a function inside event with "this"
如何调用此事件中的函数?
myfunction() {
alert('bla bla!');
}
this.map.on('moveend', function() {
console.log('moveend :: ' + this.getCenter());
this.myfunction(); // => ERROR TypeError: this.myfunction is not a function
});
尝试以下方法
this.map.on('moveend', () => {
console.log('moveend :: ' + this.getCenter());
this.myfunction();
});
应该可以。
注意:您在这里遇到的问题是 scope。我建议您阅读一下,这是一个很有价值的概念。
如何调用此事件中的函数?
myfunction() {
alert('bla bla!');
}
this.map.on('moveend', function() {
console.log('moveend :: ' + this.getCenter());
this.myfunction(); // => ERROR TypeError: this.myfunction is not a function
});
尝试以下方法
this.map.on('moveend', () => {
console.log('moveend :: ' + this.getCenter());
this.myfunction();
});
应该可以。
注意:您在这里遇到的问题是 scope。我建议您阅读一下,这是一个很有价值的概念。