为什么示例不起作用? (与进口的斗争)

编程入门 行业动态 更新时间:2024-10-28 00:25:37
本文介绍了为什么示例不起作用? (与进口的斗争)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

On the page openlayers/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:

  • SyntaxError:导入声明只能出现在模块的顶层
  • 未声明HTML文档的字符编码.文件...
  • 要修复它:

  • 将<script>替换为<script type="module">
  • 将<meta charset="UTF-8">添加到<head></head>
  • replace <script> by <script type="module">
  • 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种方法:

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

    我还使用此示例

    您始终可以使用旧的方式,例如在版本4.6.5中使用其他官方教程,而无需使用import.

    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提交了一些建议,例如 github/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 github/openlayers/openlayers/issues/8324

  • 更多推荐

    为什么示例不起作用? (与进口的斗争)

    本文发布于:2023-11-15 07:04:08,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1592212.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:示例   不起作用

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!