首页 > 解决方案 > 反应传单找不到'pathtofile'的路径

问题描述

我有一个 geojson 文件,其中包含此类图标的路径。

"features": [
      {
        "type": "Feature",
        "properties": {
            "ICON": ".././assets/images/marker-icon.png"

我正在映射geojson文件并尝试使用以下方式设置图标路径:

const markers = geojson.features.map((features, i) => {
    const greenIcon = L.icon({
      iconUrl: require(`${features.properties.ICON}`)
    })

但是我在控制台中收到以下错误

Cannot find module '.././assets/images/marker-icon.png'

我试图理解为什么会这样:

  iconUrl: require(.././assets/images/marker-icon.png)

但是当我映射这不会:

iconUrl: require(`${features.properties.ICON}`)

标签: reactjsreact-leaflet

解决方案


我将图像路径“ICON”的geojson更改为:

"ICON": "marker-icon.png"

然后改变以下工作:

iconUrl: require(`.././assets/images/${features.properties.ICON}`)

推荐阅读