我正在使用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 };

当前回答

对我来说,这是一个编译问题。我已经添加了

  "devDependencies": {
    ...
    "@babel/cli": "^7.7.5",
    "@babel/core": "^7.7.5",
    "@babel/node": "^7.7.4",
    "@babel/plugin-proposal-class-properties": "^7.7.4",
    "@babel/plugin-transform-instanceof": "^7.8.3",
    "@babel/plugin-transform-runtime": "^7.7.6",
    "@babel/preset-env": "^7.7.5",
    "@babel/register": "^7.7.4",
    "@babel/runtime": "^7.9.6"
  },


  "dependencies": {
    ...
    "@babel/plugin-transform-classes": "^7.15.4"
  },

增加了.babelrc文件

{
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-transform-instanceof",
    "@babel/plugin-transform-classes"
  ],
  "presets": ["@babel/preset-env"],
  "env": {
    "test": {
      "plugins": ["@babel/plugin-transform-runtime"]
    }
  }
}

其他回答

我只是添加了"type": "module"到我的包。Json文件,它为我工作。

触发此错误是因为您在HTML文件中链接到的文件是该文件的未绑定版本。 要获得完整的捆绑版本,你必须安装npm:

npm install --save milsymbol

这将把完整的包下载到node_modules文件夹中。

然后,您可以在node_modules/milsymbol/dist/milsymbol.js中访问独立的微型JavaScript文件

您可以在任何目录中执行此操作,然后将下面的文件复制到/src目录。

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

以前是这样的

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

在把它改成

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

它工作得很完美。

对我来说,这是由于没有引用一个库(特别是typeORM,使用ormconfig.js文件,在实体键下)到src文件夹,而不是dist文件夹…

   "entities": [
      "src/db/entity/**/*.ts", // Pay attention to "src" and "ts" (this is wrong)
   ],

而不是

   "entities": [
      "dist/db/entity/**/*.js", // Pay attention to "dist" and "js" (this is the correct way)
   ],

如果您想对模块使用import而不是require(),请更改或添加package中的type值到module。json文件

例子:

包中。json文件

{
  "name": "appsample",
  "version": "1.0.0",
  "type": "module",
  "description": "Learning Node",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Chikeluba Anusionwu",
  "license": "ISC"
}
import http from 'http';

var host = '127.0.0.1',
    port = 1992,
    server = http.createServer();

server.on('request', (req, res) => {
  res.writeHead(200, {"Content-Type": "text/plain"});
  res.end("I am using type module in package.json file in this application.");
});

server.listen(port, () => console.log(
    'Listening to server ${port}. Connection has been established.'));