Nan*_*amy 3 javascript callback typescript angular angular5
我在 Angular 5 前端框架中使用javascript Google Maps组件
export class MapComponent implements OnInit {
ngOnInit() {
this.initializeGMap()
}
initializeGMap() {
var myLatlng = new google.maps.LatLng(12,77);
var mapOptions = {
zoom: DEFAULT_MAP_ZOOM,
center: myLatlng,
scrollwheel: true,
styles: MAP_STYLE
};
this.map = new google.maps.Map(document.getElementById("map"), mapOptions);
this.initOnMapClickListener();
}
initOnMapClickListener() {
google.maps.event.addListener(this.map, 'click', function(event) {
var selectedLocation = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
this.addMarker(selectedLocation)
});
}
addMarker(latlng) {
var marker = new google.maps.Marker({
position: latlng,
map: this.map,
icon: './assets/map_marker.png'
});
}
}
Run Code Online (Sandbox Code Playgroud)
上面是我的打字稿文件,它有三个功能
未捕获的类型错误:无法读取 null 的属性“addMarker”
如果我运行 Angular 应用程序,这就是我收到的控制台错误
请帮助理解如何在 Javascript 回调中调用 typescript 函数
尝试使用像这样的箭头函数调用函数来绑定到-=>的松弛范围this
initOnMapClickListener() {
google.maps.event.addListener(this.map, 'click', (event) => {
var selectedLocation = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
this.addMarker(selectedLocation)
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1928 次 |
| 最近记录: |