为什么示例不起作用? (与进口的斗争) [英] Why examples don't work? (a struggle with imports)

查看:81
本文介绍了为什么示例不起作用? (与进口的斗争)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在页面上 https://openlayers.org/en/latest /examples/image-vector-layer.html 我将HTML代码从地图下方复制到/tmp/a.html并运行firefox /tmp/a.html.

On the page https://openlayers.org/en/latest/examples/image-vector-layer.html I copied the HTML code from under the map to /tmp/a.html and run firefox /tmp/a.html.

最初,两个问题似乎很容易解决:

At first two problems appeared easy to fix:

  1. SyntaxError:导入声明只能出现在模块的顶层
  2. 未声明HTML文档的字符编码.文件...

要修复它:

  1. <script>替换为<script type="module">
  2. <meta charset="UTF-8">添加到<head></head>
  1. replace <script> by <script type="module">
  2. add <meta charset="UTF-8"> into <head></head>

但是第三个错误该怎么办?

But what to do with the third error?

TypeError: Error resolving module specifier: ol/Map.js

我有Firefox 60.0.1.

I have Firefox 60.0.1.

那么,示例中的HTML代码是按原样使用还是我误解了?

So, are the HTML codes in the examples meant to be used as I did, or did I misunderstand something?

我在import Map from ol/Map.js的代码中需要什么?

And what do I need in my code to import Map from ol/Map.js?

(我试图重新表述这个问题,但是如果我仍然应该获得负面排名,请解释原因.谢谢)

(I tried to reformulate the question, but if I still deserve a negative ranking, please explain why. Thanks)

推荐答案

这是因为由于最新版本的OpenLayers(V5.0)导致了一些更改. 现在,示例基于ES6模块,而以前有另一种方法

It's because there are some changes due to latest release of OpenLayers (V5.0). Now samples are based on ES6 modules whereas there was before another way of doing

您可以将简单" v4.6.5示例简单"主样本

仅使用<script type="module">是不够的,因为这样做不能解决import Map from ol/Map.js

Using <script type="module"> is not enough as it does not solve dependencies when doing import Map from ol/Map.js

至少有3种方法:

  1. 使用5.0.0版创建Openlayers示例的通常方法是使用诸如Webpack或Parcel之类的捆绑器.有有关此问题的教程.

我还使用此示例

您始终可以使用旧的方式,例如在版本4.6.5中使用

You can always use the old way of doing, like in version 4.6.5 using this other official tutorial without using import.

对于解决方案1,您可以使用codesandbox.io避免设置本地包裹/webpack环境,如此推文.

For solution 1, you can use codesandbox.io to avoid setting a local parcel/webpack environment like illustrated with this tweet.

我知道有一个正在进行的重构示例代码的工作,并且我还为codeandbox.io提交了一些建议,例如 https://github.com/openlayers/openlayers/issues/8324

I know there is a work on progress to refactor the code for samples and I also submitted some suggestions for codesandbox.io e.g https://github.com/openlayers/openlayers/issues/8324

这篇关于为什么示例不起作用? (与进口的斗争)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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