我正在使用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"]
}
}
}
对我来说,我不想更新我的包。Json文件,并将文件类型更改为mjs。
所以我四处寻找,发现在tsconfig文件中更改模块。Json会影响结果。我的ts.config文件是:
{
"compilerOptions": {
"target": "es2020",
"module": "es2020",
"lib": [
"es2020",
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": "."
},
"exclude": [
"node_modules"
],
"include": [
"./src/**/*.ts"
]
}
像这样,将模块从"module": "es2020"改为"module": "commonjs"解决了我的问题。
我正在使用MikroORM,并认为它可能不支持任何CommonJS以上的模块。