我需要帮助以从React-Leaflet创建GeoJSON自定义组件
使用React和React-Leaflet编写(均为最新版本) 在Map组件中编写代码时,该代码有效,但我想将其导入/导出以拆分代码
import React from 'react';
import { withLeaflet, GeoJSON } from 'react-leaflet'
import L from 'leaflet'
class CustomGesJSON extends GeoJSON {
getStyle(feature) {
// some code
}
pointToLayer(feature, latlng) {
// some code
}
onEachFeature(feature, layer) {
// some code
}
createLeafletElement(opts) {
const CustomGesJSON = L.geoJSON.extend({
onAdd: (map) => {
this.getStyle = this.getStyle.bind(this);
this.pointToLayer = this.pointToLayer.bind(this);
this.onEachFeature = this.onEachFeature.bind(this);
return this ;
}
});
return new CustomGesJSON({ data: this.props.data });
}
}
function testlog(txt) {
// some code
}
export default withLeaflet(CustomGesJSON);
我收到一条错误消息“ GeoJSON不是构造函数”
函数和方法(此处未显示)有效,我只需要帮助以进行适当的继承 欢迎使用其他解决方案
感谢您的帮助
答案 0 :(得分:0)
“ react-leaflet”导出的“ GeoJSON”对象可能不是ES6类,而是Leaflet L.GeoJSON“类”。
您可以使用Leaflet自己的ES6之前的类继承方案,如Leaflet class theory tutorial中所述:
const MyCustomClass = GeoJSON.extend({
options: {
onEachFeature: myCustomDefaultFunction
// etc.
}
});
export default MyCustomClass;