Vue2Leaflet是一个在Vue2框架中实现Leaflet的库;能够在地图上显示GeoJSON对象。
对于多个GeoJSON行,我希望有一个影响其他行的样式的鼠标单击事件(例如,它切换了一个selectedLineId
变量)。我设法在鼠标悬停时更改了geojson线的样式;请参见此JSFiddle。
核心是onEachFeature
,它将鼠标悬停事件添加到每个功能上。但是我不知道如何在这里运行Vue方法;
function onEachFeature (feature, layer) {
layer.on('mouseover', function (e) {
e.target.setStyle({
color: "#FF0000"
});
});
layer.on('mouseout', function (e) {
e.target.setStyle({
color: "#000000"
});
});
}
发布于 2018-07-25 08:41:13
您可以将onEachFeature
函数bind到您的Vue对象:
data() {
return {
// ...
lines: {
geojson: geojsondata,
options: {
onEachFeature: onEachFeature.bind(this),
style: {
color: "#000000"
}
}
}
}
}
然后,onEachFeature
中的this
将引用您的Vue对象:
function onEachFeature (feature, layer) {
var v = this;
layer.on('mouseover', function (e) {
// assuming you have a getColor method defined
e.target.setStyle({
color: v.getColor()
});
});
layer.on('mouseout', function (e) {
e.target.setStyle({
color: "#000000"
});
});
}
这是一个更新的提琴:https://jsfiddle.net/qywaz1h8/96/
https://stackoverflow.com/questions/51513848
复制相似问题