浏览器中的 ES6 模块:Uncaught SyntaxError: Unexpected token import

新手上路,请多包涵

我是 ES6 (ECMAScript 6)的新手,我想在浏览器中使用它的 模块系统。我读到 ES6 受 Firefox 和 Chrome 支持,但使用 export

Uncaught SyntaxError: Unexpected token import

我有一个 test.html 文件

<html>
    <script src="test.js"></script>
<body>
</body>
</html>

和一个 test.js 文件

'use strict';

class Test {

    static hello() {
        console.log("hello world");
    }
}

export Test;

为什么?

原文由 cdarwin 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 604
2 个回答

许多现代浏览器现在都支持 ES6 模块。只要您使用 <script type="module" src="..."> 导入脚本(包括应用程序的入口点),它就可以工作。

查看 caniuse.com 了解更多详情: https ://caniuse.com/#feat=es6-module

原文由 vullnetyy 发布,翻译遵循 CC BY-SA 4.0 许可协议

您可以在 Google Chrome Beta (61) / Chrome Canary 中试用 ES6 模块。

Paul Irish 的 ToDo MVC 参考实现 - https://paulirish.github.io/es-modules-todomvc/

我有基本的演示 -

 //app.js
import {sum} from './calc.js'

console.log(sum(2,3));

 //calc.js
let sum = (a,b) => { return a + b; }

export {sum};

 <html>
    <head>
        <meta charset="utf-8" />
    </head>

    <body>
        <h1>ES6</h1>
        <script src="app.js" type="module"></script>
    </body>

</html>

希望能帮助到你!

原文由 Roopesh Reddy 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题