Javascript 加载shapefile时出现传单错误(未定义shp)

Javascript 加载shapefile时出现传单错误(未定义shp),javascript,leaflet,shapefile,Javascript,Leaflet,Shapefile,我正在关注关于如何加载ShapeFile的。 本书的说明如下: 首先,添加对两个JavaScript文件的引用。第一,, 传单文件,是插件,第二个取决于 shapefile解析器,shp.js: 自从我上了Webpack之后,我按照上面的说明做了一些细微的改变。以下是我所做的: 使用npm i shpjs--S安装的shpjs 从下载的传单.shpfile.js并将其放置在src/ 在我的*.js文件中,我需要shpjs和shaple.shpfile.js以及以下代码: 然后,我按照说明访问sh

我正在关注关于如何加载ShapeFile的。 本书的说明如下:

首先,添加对两个JavaScript文件的引用。第一,, 传单文件,是插件,第二个取决于 shapefile解析器,shp.js:

自从我上了Webpack之后,我按照上面的说明做了一些细微的改变。以下是我所做的:

  • 使用npm i shpjs--S安装的
    shpjs
  • 从下载的
    传单.shpfile.js
    并将其放置在
    src/
  • 在我的
    *.js
    文件中,我需要
    shpjs
    shaple.shpfile.js
    以及以下代码:
  • 然后,我按照说明访问shapefile:

    console.log(shp);
    const shapeFileLayer = L.shapefile(someURL);
    
    在最后一行代码中,我得到以下跟踪:

    react-dom.production.min.js:196 ReferenceError: shp is not defined
        at NewClass.addFileData (leaflet.shpfile.js:38)
        at NewClass.initialize (leaflet.shpfile.js:25)
        at new NewClass (leaflet-src.js:300)
        at Object.L.shapefile (leaflet.shpfile.js:67)
        at Map.createLayerGroups (main.js_+_3_modules:342)
        at Map.componentDidMount (main.js_+_3_modules:186)
        at ik (react-dom.production.min.js:251)
        at exports.unstable_runWithPriority (scheduler.production.min.js:18)
        at fg (react-dom.production.min.js:120)
        at Yj (react-dom.production.min.js:244)
    

    紧靠上一行的
    console.log
    语句成功,表明定义了一些函数
    shp
    。因此,我无法解释为什么我在接下来的一行中没有定义参考错误
    shp

    如果您使用的是
    require
    ,那么这意味着您使用的是AMD模块,并且应该使用某种捆绑(webpack、rollup、babel等)。您在这方面的设置是什么?请注意,假设
    shp
    是一个全局变量。也许你可以利用这些信息来解决这个问题。@IvanSanchez
    window.shp=shp
    似乎确实解决了这个问题(它在道路上越走越远,但至少这个障碍已经跨越了)。通过第14、16和35行的和对全局
    shp
    的引用,您是如何推断
    sloop.shpfile.js
    期望
    shp
    是全局的。此外,某个时代的传单plgins要求您连续加载多个JS文件,它们倾向于依赖globals或
    L
    global中的内容。
    
    const shp=require('shpjs');
    require('./leaflet.shpfile.js');
    
    console.log(shp);
    const shapeFileLayer = L.shapefile(someURL);
    
    react-dom.production.min.js:196 ReferenceError: shp is not defined
        at NewClass.addFileData (leaflet.shpfile.js:38)
        at NewClass.initialize (leaflet.shpfile.js:25)
        at new NewClass (leaflet-src.js:300)
        at Object.L.shapefile (leaflet.shpfile.js:67)
        at Map.createLayerGroups (main.js_+_3_modules:342)
        at Map.componentDidMount (main.js_+_3_modules:186)
        at ik (react-dom.production.min.js:251)
        at exports.unstable_runWithPriority (scheduler.production.min.js:18)
        at fg (react-dom.production.min.js:120)
        at Yj (react-dom.production.min.js:244)