代码在普通 Javascript 中工作得很好,但在 typescript(Angular) 中它抛出了一个错误,即 this.map.foreachfeatuatpixel(), this.map 未定义,无法读取未定义的属性
这是我的代码:
public selected: Collection<Feature<any>> = new Collection();
multiSelectFeatures(): void {
this.clearInteractions();
this.ClearSelection();
if (this.selected instanceof Collection) {
this.selected.forEach(function (feature) {
feature.setStyle(undefined);
});
this.selected.clear();
}
this.map.on("singleclick", this.getmultiplefeaturesatpixel);
}
getmultiplefeaturesatpixel(evt: { pixel: Pixel }): void {
this.map.forEachFeatureAtPixel(evt.pixel, (feature): void => {
if (feature instanceof Feature) {
const featureArray = this.selected.getArray();
const selIndex = featureArray.indexOf(feature);
if (selIndex === -1) {
this.selected.push(feature);
console.log(feature);
// this.selected.getArray().forEach((feat) => feat.setStyle(editOperations.highlightStyle));
} else {
this.selected.remove(feature);
feature.setStyle(undefined);
}
}
});
}
我想从 onclick 事件中选择地图上的多个要素,但是当我将单击事件传递给另一个函数时, this.map 未定义,并且出现错误无法读取未定义的属性