javascript - 使用通过 npm 安装的 ES 模块,而不使用像 parcel/webpack 这样的模块打包器
问题描述
我试图了解 ES 模块和节点模块是如何协同工作的。我想使用 OpenLayers (ol),我尝试在没有 JavaScript 模块打包器(如 parcel/webpack 或类似的)的情况下使用它。避免使用模块打包器的原因只是为了了解模块是如何工作的,以便更容易地将其集成到我的 django 应用程序中并避免另一层复杂性。但我遇到了问题,想知道是否值得花时间尝试。使用节点模块是否需要一些模块打包程序?
我的情况
我按照本教程进行操作,但我避免使用包裹。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Using Parcel with OpenLayers</title>
<link rel="stylesheet" href="./node_modules/ol/ol.css">
<style>
#map {
width: 400px;
height: 250px;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="module" src="./node_modules/ol/Map.js"></script>
<script type="module" src="./node_modules/ol/View.js"></script>
<script type="module" src="./node_modules/ol/layer/Tile.js"></script>
<script type="module" src="./node_modules/ol/source/OSM.js"></script>
<script type="module">
import Map from './node_modules/ol/Map.js';
import View from './node_modules/ol/View.js';
import TileLayer from './node_modules/ol/layer/Tile.js';
import OSM from './node_modules/ol/source/OSM.js';
const map = new Map({
target: 'map',
layers: [
new TileLayer({
source: new OSM()
})
],
view: new View({
center: [0, 0],
zoom: 0
})
});
</script>
</body>
</html>
我明白了TypeError: Error resolving module specifier: rbush
。所以我猜 openlayers 很大程度上依赖于一些打包工具,没有它就无法使用。那正确吗?
解决方案
推荐阅读
- verilog - 在case语句系统verilog中生成块
- sql - 存储函数值
- javascript - 如何解析从webapi返回的json日期时间以使用ajax以yyyy/mm/dd格式在前端打印
- datetime - Dynamics CRM 365 本地统一界面 webresource 从表单中读取日期字段以 UTC 获取日期
- python - 第三个括号内的双冒号 [::] 是什么意思?
- sql - 在 vertica 中编写 udf 的最佳方法,我需要从其中一个费率表中引用数据并在顶部写一个公式
- linux - 如何在 shell 脚本中使用命令输出作为 grep 的输入
- java - Java - yahtzee 的小动画 - 改变图像
- php - 使用 php 获取 Flipkart 卖家账户的访问令牌
- python - 为什么寻求属性不能在 chrome 浏览器上工作..??但是它可以在 IE 上运行