“未捕获的语法错误:无法在模块外使用导入语句";导入 ECMAScript 6 时 [英] "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6
问题描述
我正在使用 ArcGIS JSAPI 4.12 并希望使用 Spatial Illusions 在地图上绘制军事符号.
I'm using ArcGIS JSAPI 4.12 and wish to use Spatial Illusions to draw military symbols on a map.
当我将 milsymbol.js
添加到脚本中时,控制台返回错误
When I add milsymbol.js
to the script, the console returns error
未捕获的语法错误:不能在模块外使用导入语句`
Uncaught SyntaxError: Cannot use import statement outside a module`
所以我将 type="module"
添加到脚本中,然后它返回
so I add type="module"
to the script, and then it returns
未捕获的引用错误:ms 未定义
Uncaught ReferenceError: ms is not defined
这是我的代码:
<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"
.我现在真的很困惑.他们如何在不添加类型的情况下使其工作?
So, whether I add type="module"
or not, there are always errors. However, in the official document of Spatial Illusions, there isn't any type="module"
in the script. I'm now really confused. How do they manage to get it work without adding the type?
import { ms } from "./ms.js";
import Symbol from "./ms/symbol.js";
ms.Symbol = Symbol;
export { ms };
推荐答案
我收到这个错误是因为我忘记了脚本标签中的 type=module"
:
I got this error because I forgot the type="module"
inside the script tag:
<script type="module" src="milsymbol-2.0.0/src/milsymbol.js"></script>
这篇关于“未捕获的语法错误:无法在模块外使用导入语句";导入 ECMAScript 6 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!