this.$nextTick(() => { for (var i = 0; i < allMarker.length; i++) { if (allMarker[i].id == this.markerId) { const markerBig = allMarker[i] const iconSize = markerBig.getIcon() console.log(iconSize, 'iconSize') iconSize.setImageSize({ width: 48, height: 48 }) iconSize.setSize({ width: 48, height: 48 }) iconSize.setAnchor({ width: 20, height: 45 }) markerBig.setIcon(iconSize) // const point = new BMapGL.Point(markerBig.latLng.lng, markerBig.latLng.lat) // this.map.centerAndZoom(point, 17) } } })优化一下
时间: 2024-02-17 08:00:54 浏览: 24
可以考虑将循环部分的代码提取出来,单独作为一个函数,然后在$nextTick回调函数中调用该函数。这样可以提高代码的可读性和可维护性,同时也避免了在循环中频繁调用DOM操作影响性能的问题。具体实现可以参考下面的代码:
```
function updateMarkerIcon(allMarker, markerId) {
for (var i = 0; i < allMarker.length; i++) {
if (allMarker[i].id == markerId) {
const markerBig = allMarker[i];
const iconSize = markerBig.getIcon();
console.log(iconSize, 'iconSize');
iconSize.setImageSize({
width: 48,
height: 48
});
iconSize.setSize({
width: 48,
height: 48
});
iconSize.setAnchor({
width: 20,
height: 45
});
markerBig.setIcon(iconSize);
// const point = new BMapGL.Point(markerBig.latLng.lng, markerBig.latLng.lat);
// this.map.centerAndZoom(point, 17);
}
}
}
this.$nextTick(() => {
updateMarkerIcon(allMarker, this.markerId);
});
```