我正在使用ArcGIS JSAPI 4.12,希望使用空间幻象在地图上绘制军事符号。

当我将milsymbol.js添加到脚本中时,控制台返回错误

无法在模块外部使用import语句

所以我添加type="module"到脚本中,然后它返回

Uncaught ReferenceError: ms未定义

这是我的代码:

<link rel="stylesheet" href="https://js.arcgis.com/4.12/esri/css/main.css">
<script src="https://js.arcgis.com/4.12/"></script>
<script type="module" src="milsymbol-2.0.0/src/milsymbol.js"></script>

<script>
    require([
        "esri/Map",
        "esri/views/MapView",
        "esri/layers/MapImageLayer",
        "esri/layers/FeatureLayer"
    ], function (Map, MapView, MapImageLayer, FeatureLayer) {

        var symbol = new ms.Symbol("SFG-UCI----D", { size: 30 }).asCanvas(3);
        var map = new Map({
            basemap: "topo-vector"
        });

        var view = new MapView({
            container: "viewDiv",
            map: map,
            center: [121, 23],
            zoom: 7
        });
    });
</script>

所以,无论我是否添加type="module",总会有错误。但是在Spatial Illusions的官方文档中,脚本中没有type="module"。我现在真的很困惑。他们如何在不添加类型的情况下让它工作呢?

文件milsymbol.js

import { ms } from "./ms.js";

import Symbol from "./ms/symbol.js";
ms.Symbol = Symbol;

export { ms };

当前回答

我也面临着同样的问题,直到我在脚本中添加type="module"。

以前是这样的

<script src="../src/main.js"></script>

在把它改成

<script type="module" src="../src/main.js"></script>

它工作得很完美。

其他回答

错误的原因如下:

You're currently loading the source file in the src directory instead of the built file in the dist directory (you can see what the intended distributed file is here). This means that you're using the native source code in an unaltered/unbundled state, leading to the following error: Uncaught SyntaxError: Cannot use import statement outside a module. This should be fixed by using the bundled version since the package is using rollup to create a bundle. The reason you're getting the Uncaught ReferenceError: ms is not defined error is because modules are scoped, and since you're loading the library using native modules, ms is not in the global scope and is therefore not accessible in the following script tag.

看起来你应该能够加载这个文件的dist版本,在窗口上定义ms。请查看库作者提供的这个示例,以了解如何做到这一点。

为什么会出现这种情况以及更多可能的原因:

很多接口仍然不理解ES6 JavaScript语法/特性。因此,无论在任何文件或项目中使用ES6,都需要将ES6编译为ES5。

出现SyntaxError:不能在模块外使用import语句错误的可能原因是您试图独立运行该文件。您还没有安装和设置ES6编译器(如Babel),或者您的runscript中的文件路径是错误的/不是编译文件。

如果你想继续没有编译器,最好的解决方案是使用ES5语法,在你的情况下,这将是var ms = require(./ms.js);。这可以稍后更新为适当或更好的仍然设置你的编译器,并确保你的文件/项目在运行之前编译,也确保你的运行脚本正在运行编译文件,通常命名为dist, build或任何你命名的文件,在你的runscript中编译文件的路径是正确的。

我在世博会上也犯过同样的错误。

主要解决方案是在包中添加“type”:“module”。json文件。

但是,您必须检查哪个是正确的package.json。

在我的情况下,有两个包。Json文件,然后你应该把它添加到服务器文件中。

以确定哪个是正确的包。. json,找到“scripts”:{"test": "echo \"错误:没有测试指定的\" && exit 1"},

在这一行下面,添加“type”:“module”,

有几种常见的方法可以解决与上述问题相关的冲突


1. 第一个:在脚本中,包含type=module

    <script type="module" src="milsymbol-2.0.0/src/milsymbol.js"></script>

2. 第二种:在node.js中,放入你的包中。json文件

    {
       
        "type": "module",
       
    }

重新启动项目npm start


3.第三:按要求替换导入

试试这个

import { parse } from 'node-html-parser';
parse = require('node-html-parser');

否则试试这个

//import { parse } from 'node-html-parser';
parse = require('node-html-parser');

在我的情况下,我更新了

"lib": [
      "es2020",
      "dom"
    ]

with

"lib": [
  "es2016",
  "dom"
]

在我的tsconfig中。json文件。