javascript - 如何从 html 文件调用函数到 js 类型的模块文件?
问题描述
首先,感谢您阅读我的问题并尝试帮助我并为我的英语道歉。
我有以下问题...
我想从 html 中执行我创建的 js 文件的函数,向该文件传递两个参数:地图的名称和应加载地图的 id。但是作为一个模块类型告诉我它没有定义。但是,如果该文件 js 不是模块类型并且如果它识别并执行它则没有任何导入。
我的代码如下...
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Map Generator</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script type="module" src='./js/index.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.46.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.46.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; height:100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
customMap.addMap("icgc", "map");
</script>
</body>
</html>
index.js 文件
import MapFunctions from './class/MapFunctions.js'
// new MapFunctions().createMap("openstreetmaps", "map");
var customMap = {
addMap: function name(base, target) {
new MapFunctions().createMap(base, target);
}
}
使用构造函数它可以工作,但目的是从 index.html 中您只需调用函数 customMap.addMap () 并且只导入文件 index.js
在这里你有存储库,以防你在那里更清楚地看到它
谢谢
解决方案
推荐阅读
- sql - oracle - 仅当它包含该 ID 的最大日期值时,才选择标记为 1 的所有不同 ID
- docker - 无法将 Apache Cassandra 与 DataGrip 连接
- node.js - 基于节点的命令行应用程序的汇总不捆绑代码
- html - 粘性 CSS 设置不起作用,因为祖先 div 没有填充可用高度
- typo3 - TYPO3 10 & tx_news 版本 9.0.0 分类新闻列表的分页
- reactjs - 如何使用 React 顺序显示项目列表
- python - Python - 将参数传递给从 TestCase 继承的基类
- css - SAPUI5 - 输入字段 - 字体大小 - CSS:更改字体大小不起作用
- reactjs - 将上下文状态传递给类?
- python - 运行 python manage.py install_labels 时 Django-neomodels 驱动程序错误