浏览器中的 ES6 模块:Uncaught SyntaxError: Unexpected token import [英] ES6 modules in the browser: Uncaught SyntaxError: Unexpected token import
问题描述
我是 ES6 (ECMAScript 6) 的新手,我想在浏览器中使用它的模块系统.我读到 Firefox 和 Chrome 支持 ES6,但使用 export
I'm new to ES6 (ECMAScript 6), and I'd like to use its module system in the browser. I read ES6 is supported by Firefox and Chrome, but I'm getting the following error using export
Uncaught SyntaxError: Unexpected token import
我有一个 test.html 文件
I have a test.html file
<html>
<script src="test.js"></script>
<body>
</body>
</html>
和一个 test.js 文件
and a test.js file
'use strict';
class Test {
static hello() {
console.log("hello world");
}
}
export Test;
为什么?
推荐答案
许多现代浏览器现在都支持 ES6 模块.只要您使用 <script type="module" src="...">
导入脚本(包括应用程序的入口点),它就会起作用.
Many modern browsers now support ES6 modules. As long as you import your scripts (including the entrypoint to your application) using <script type="module" src="...">
it will work.
查看 caniuse.com 了解更多详情:https://caniuse.com/#feat=es6-module
Take a look at caniuse.com for more details: https://caniuse.com/#feat=es6-module
这篇关于浏览器中的 ES6 模块:Uncaught SyntaxError: Unexpected token import的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!