“未捕获的SyntaxError:无法在模块外部使用import语句"导入ECMAScript 6时 [英] "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6

查看:855
本文介绍了“未捕获的SyntaxError:无法在模块外部使用import语句"导入ECMAScript 6时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

未捕获的SyntaxError:无法在模块外部使用import语句.

Uncaught SyntaxError: Cannot use import statement outside a module`

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

so I add type="module" to the script, and then it returns

未捕获的ReferenceError:未定义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",总是存在错误.但是,在空间幻象的正式文档中,脚本中没有任何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>

这篇关于“未捕获的SyntaxError:无法在模块外部使用import语句"导入ECMAScript 6时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆