当我在本地运行
gatsby develop
时,我的传单地图中的自定义图标工作得很好,但是当我尝试构建时,我收到与我的自定义标记图标相关的错误:
WebpackError: TypeError: leaflet_src_layer_marker__WEBPACK_IMPORTED_MODULE_4__.Icon is not a constructor
相关进口:
import { Map, Marker, Popup, TileLayer, Tooltip, ZoomControl } from 'react-leaflet'
import { Icon } from 'leaflet/src/layer/marker'
创建自定义图标:
// Init custom map icon
const mapIcon = MapIcon();
const markerIcon = new Icon({
iconUrl: mapIcon,
iconSize: [36, 36],
iconAnchor: [18, 18],
shadowSize: [36, 36],
shadowUrl: 'https://unpkg.com/[email protected]/dist/images/marker-shadow.png',
shadowAnchor: [10, 18],
popupAnchor: [0, -16],
tooltipAnchor: [13, -4]
});
JSX:
{ markerData.length > 0 ? markerData.map((node, index) => (
<Marker position={[node.coords.lat, node.coords.lng]} key={`marker-${index}`} icon={markerIcon}>
<Popup className="leaflet-popup" onOpen={(el) => openPopup(el)}>
<h5 className="popup-location-title">{node.location_title}</h5>
<h6 className="popup-address">{node.address}</h6>
<div className="popup-description" dangerouslySetInnerHTML={{ __html: node.description }}></div>
{!!node.embed ?
<div className="popup-embed">
<Embed url={node.embed} className="popup-media-embed" />
</div>
: null}
</Popup>
<Tooltip className="leaflet-tooltip">
<span className="title">{node.location_title}</span>
<span className="address">{node.address}</span>
</Tooltip>
</Marker>
)) : null }
我已经尝试了此处列出的所有解决方案:https://github.com/Leaflet/Leaflet.markercluster/issues/874。我也看过几个类似的问题。似乎没有什么帮助。例如,我也尝试像这样
import * as L from "leaflet"
和像这样import L from "leaflet"
导入传单。然后创建新图标,像这样const markerIcon = L.Icon({
,像这样const markerIcon = L.icon({
,像这样const markerIcon = new L.Icon({
,像这样const markerIcon = new L.icon({
。我可以console.log(L)
,但这些都不起作用。
我希望得到一些帮助。
可以构建它了。我必须将图标结构包装在窗口对象的检查中:
// Init custom map icon
if (typeof window !== 'undefined') {
const mapIcon = MapIcon();
markerIcon = new Icon({
iconUrl: mapIcon,
iconSize: [36, 36],
iconAnchor: [18, 18],
shadowSize: [36, 36],
shadowUrl: 'https://unpkg.com/[email protected]/dist/images/marker-shadow.png',
shadowAnchor: [10, 18],
popupAnchor: [0, -16],
tooltipAnchor: [13, -4]
});
}
再次将图标传递给标记时:
<Marker position={[node.coords.lat, node.coords.lng]} key={`marker-${index}`} icon={(!!markerIcon) ? markerIcon : null}>
in my case i had to wrap the icon with this form
let icon = ""
if(typeof window !== 'undefined'){
icon = Leaflet.icon({
iconUrl: PointerIcon,
iconSize: [38, 38],
});
}
and add the same validation in the component that is calling it