简体   繁体   English

Angular 中的开放层

[英]Openlayers in Angular

I have a click function on an OpenLayers map我在 OpenLayers map 上单击 function

testvar: string
map = new Map()


ngOnInit(): void {

this.map.on('click', function(event) {
   console.log(this.testvar)
})

}

But this is not available within the scope of the click function. this在 function 的 scope 内不可用。

Any ideas how to do this/任何想法如何做到这一点/

this is because you declare your function using the function keyword.这是因为您使用function关键字声明了 function。 If you declare you function as an arrow function, it will work:如果您将 function 声明为箭头 function,它将起作用:

this.map.on('click', event => {
   console.log(this.testvar)
})

Take a look at this article for more detail about arrow function vs function: https://www.freecodecamp.org/news/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26/ Take a look at this article for more detail about arrow function vs function: https://www.freecodecamp.org/news/when-and-why-you-should-use-es6-arrow-functions-and-when-you -应该-3d851d7f0b26/

or this one: https://dmitripavlutin.com/differences-between-arrow-and-regular-functions/或者这个: https://dmitripavlutin.com/differences-between-arrow-and-regular-functions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM