我是ES6 (ECMAScript 6)的新手,我想在浏览器中使用它的模块system。我读到火狐和Chrome支持ES6,但我在使用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;
为什么?
发布于 2018-12-02 22:54:51
许多现代浏览器现在都支持ES6模块。只要您使用<script type="module" src="...">
导入脚本(包括应用程序的入口点),它就可以工作。
有关更多详细信息,请查看caniuse.com:https://caniuse.com/#feat=es6-module
发布于 2017-08-18 06:37:27
你可以在Google Chrome Beta (61) / Chrome Canary中尝试ES6模块。
保罗·爱尔兰的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>
希望它能帮上忙!
发布于 2018-09-21 21:01:06
将type="module"
添加到我的mjs的脚本import
中,这对我很有效:
<script type="module">
import * as module from 'https://rawgit.com/abernier/7ce9df53ac9ec00419634ca3f9e3f772/raw/eec68248454e1343e111f464e666afd722a65fe2/mymod.mjs'
console.log(module.default()) // Prints: Hi from the default export!
</script>
https://stackoverflow.com/questions/41722621
复制相似问题