Leaflet自定义图标,react-leaflet,无法构建Gatsby项目

bro*_*ger 2 leaflet gatsby react-leaflet

当我在本地运行时,我的传单地图中的自定义图标工作得很好gatsby develop,但是当我尝试构建时,我收到与自定义标记图标相关的错误:

 WebpackError: TypeError: leaflet_src_layer_marker__WEBPACK_IMPORTED_MODULE_4__.Icon is not a constructor
Run Code Online (Sandbox Code Playgroud)

相关进口:

import { Map, Marker, Popup, TileLayer, Tooltip, ZoomControl } from 'react-leaflet'
import { Icon } from 'leaflet/src/layer/marker' 
Run Code Online (Sandbox Code Playgroud)

创建自定义图标:

// 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/leaflet@1.5.1/dist/images/marker-shadow.png',
    shadowAnchor: [10, 18],
    popupAnchor: [0, -16],
    tooltipAnchor: [13, -4]
  });
Run Code Online (Sandbox Code Playgroud)

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 }
Run Code Online (Sandbox Code Playgroud)

我已经尝试了此处列出的所有解决方案: https: //github.com/Leaflet/Leaflet.markercluster/issues/874。我也看过几个类似的问题。似乎没有什么帮助。例如,我还尝试像 soimport * as L from "leaflet"和 like so导入传单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),但这些都不起作用。

我希望得到一些帮助。

bro*_*ger 7

得到它来构建。我必须将图标构造包装在窗口对象的检查中:

// 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/leaflet@1.5.1/dist/images/marker-shadow.png',
      shadowAnchor: [10, 18],
      popupAnchor: [0, -16],
      tooltipAnchor: [13, -4]
    });
  }
Run Code Online (Sandbox Code Playgroud)

当将图标传递给标记时再次:

<Marker position={[node.coords.lat, node.coords.lng]} key={`marker-${index}`} icon={(!!markerIcon) ? markerIcon : null}>
Run Code Online (Sandbox Code Playgroud)