es6 导入和导出中的模块说明符

标签 module ecmascript-6 es6-modules es6-module-loader

我对这些语句中的模块说明符所指的内容感到困惑:

export {bar} from "foo";

import {bar} from "foo";
"foo" 是什么意思引用?它不能是文件,因为它类似于 "./foo" .如果它不是一个文件,我认为它是一个 ID。 ID是如何定义的?

我正在从 js 文件导出,但导入将是 firefox 浏览器中内联 html 脚本 ( type="module" ) 的一部分。

浏览器版本(和浏览器设置)已经过验证,可以与 es6 模块一起使用。

提前致谢。

最佳答案

ES6 没有指定模块说明符所指的内容。
确实就是这样:一个标识符。而已。

由环境将这些标识符解析为实际模块。加载器可能会将它们解释为相对文件路径、全局 id、npm 模块名称或其他任何内容。

在浏览器中, <script type="module"> took some time to specify ,但它终于来了。 "foo" 的模块说明符当前无效,浏览器将忽略该模块,因为它不知道如何处理它。它将需要解析为要加载的 URL 的内容。 jack ·阿奇博尔德 wrapped it up succinctly :

"Bare" import specifiers aren't currently supported. Valid module specifiers must match one of the following:

  • A full non-relative URL. As in, it doesn't throw an error when put through new URL(moduleSpecifier).
  • Starts with /.
  • Starts with ./.
  • Starts with ../.

Other specifiers are reserved for future-use, such as importing built-in modules.

关于es6 导入和导出中的模块说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44930637/

相关文章:

Python 使用全局 vs 指定模块

javascript - 网络包错误 : output filename not configured

ruby - 模块嵌套在 instance_eval/exec 或 module_eval/exec 中

javascript - 使用用 ES6 编写的 npm 模块和 react-scripts

javascript - 将 Parquet 转换为常规 TXT 文件的最快方法是什么?

javascript - 使用 async/await 动态生成 promise 链

node.js - Apollo 服务器 Playground 无法在 heroku 上运行(本地工作)

javascript - 使用 Jasmine 或任何其他替代方案在 Node 上运行测试 .mjs/ESM

javascript - 在 HTML 文件中加载 JavaScript 会在本地环境中引发跨源请求错误,仅适用于 ES6 模块,而不适用于旧 JS,为什么?

javascript - 未捕获的语法错误 : Cannot use import statement outside a module in node js