我们如何在现代Web浏览器中使用es6模块而不转换代码 [英] How can we use es6 modules in modern web browsers without transpiling the code
问题描述
是否可以使用es6模块系统并在Web浏览器中导入模块?
Is it possible to use es6 module system and import modules in web browsers?
推荐答案
借助chrome 61,es6模块系统被引入了Web浏览器领域.此链接 https://developers.google.com/web/fundamentals/primers/modules <有关chrome如何实现模块系统的更多详细信息,请参见/a>.
With chrome 61, es6 module system is introduced to the world of web browsers. This link https://developers.google.com/web/fundamentals/primers/modules can be referred for more details on how chrome implements the module system.
上面链接中讨论的es6模块系统的一些重要功能是:
Some of the important features of es6 module system discussed in the above link are:
- 默认情况下,模块为
strict
模式. - 它们不是词法顶级范围,模块文件中的var foo = 42将无法作为window.foo使用.
- 模块以
<script type="module" src="example.mjs">
的形式包含在html文件中
- 默认情况下推迟模块
- Modules by default are
strict
mode. - They are not lexical top-level scope, which is var foo = 42 in module file won't be available as window.foo.
- Modules are included in html file as
<script type="module" src="example.mjs">
- Modules are deferred by default
还有其他一些浏览器也提供了不同版本的解决方案.下面给出的链接说明了不同浏览器中模块系统的兼容性级别. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import#Browser_compatibility
There are some other browsers too which have come up with solutions in their different versions. This below-given link explains the compatibility level of module system in different browsers. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Browser_compatibility
这篇关于我们如何在现代Web浏览器中使用es6模块而不转换代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!