es6导入和导出中的模块说明符 [英] module specifier in es6 import and export

查看:88
本文介绍了es6导入和导出中的模块说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这些语句中的模块说明符是什么,我感到困惑:

I'm confused about what the module specifier in these statements refer to:

export {bar} from "foo";

import {bar} from "foo";

"foo"是什么意思?它不能是文件,因为它类似于"./foo"之类的东西.如果不是文件,那么我认为它是一个ID.如何定义ID?

What does "foo" refer to? It can't be a file since it would be something like "./foo". If it's not a file, I assume it's an ID. How is the ID defined?

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

I'm exporting from a js file, but the import would be part of an inline html script (type="module") in the firefox browser.

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

The browser version (and browser settings) have been verified to work with es6 modules.

谢谢.

推荐答案

ES6未指定模块说明符所引用的内容.
确实只是一个标识符.没什么.

ES6 does not specify what the module specifier refers to.
It is indeed just that: an identifier. Nothing more.

由环境将这些标识符解析为实际模块.加载程序可能会将它们解释为相对文件路径,全局ID,npm模块名称以及其他任何内容.

It is left to the environment to resolve these identifiers to an actual module. A loader might interpret them as relative file paths, as global ids, as npm module names, as anything else.

在浏览器中, <script type="module">需要花费一些时间来指定,但最终就在这里. "foo"的模块说明符当前无效,浏览器将忽略该模块,因为它不知道如何处理.它将需要一些解析为URL的内容进行加载.杰克·阿奇博尔德简洁地将其包装:

In browsers, <script type="module"> took some time to specify, but it's here finally. A module specifier of "foo" is currently invalid, a browser will ignore that module as it doesn't know what to do with it. It will need something that resolves to an URL to load. Jake Archibald wrapped it up succinctly:

当前不支持裸"导入说明符.有效模块 说明符必须符合以下条件之一:

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

  • 完整的非相对URL.就像这样,放置时它不会引发错误 通过new URL(moduleSpecifier).
  • /开头.
  • ./开头.
  • ../开头.
  • 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导入和导出中的模块说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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