为了账号安全,请及时绑定邮箱和手机立即绑定

如何在浏览器中使用 ES 模块?

如何在浏览器中使用 ES 模块?

DIEA 2023-06-29 22:40:21
这是我的文件夹结构:这些是我的文件:/* index.html */<body>  <h1>Modules</h1>  <script type="module" src="./index.js"></script></body>// index.jsimport fruits from "./fruits";console.log(fruits);// fruits.jsconst arr = ["Mango", "Appple", "Banana", "Guava"];export default arr;对我来说一切看起来都很好,但我仍然收到错误:net::ERR_ABORTED 404 (Not Found)。
查看完整描述

2 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

浏览器没有 Node.js 风格的模块解析。您必须指定模块的确切 URL。

import fruits from "./fruits.js";

并删除这个:

<script type="module" src="./fruits.js"></script>

……它没有任何作用。仅当您将其导入其他地方时它才有用。


查看完整回答
反对 回复 2023-06-29
?
Qyouu

TA贡献1786条经验 获得超11个赞

尝试从“./fruits.js”导入水果;

而不是从“./fruits”导入水果;


查看完整回答
反对 回复 2023-06-29
  • 2 回答
  • 0 关注
  • 88 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信