【发布时间】:2018-08-07 06:31:19
【问题描述】:
b.js:
const x = 1;
export {x};
a.js:
import {x} from 'b'; // <<-- ERROR
console.log(x);
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="a.js"></script>
</body>
</html>
错误:
Uncaught SyntaxError: Unexpected token {
我正在使用 WebStorm 并在 Win7 的 Chrome 中运行该项目。
更新:
我将index.html的内容改为:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="a.js" type="module"></script>
</body>
</html>
错误:
好像b.js没有加载。
【问题讨论】:
-
您需要在脚本标签中包含
type="module"。其他资源 (b.js) 必须对页面可用。 -
这个问题很可能是 ES6 模块在 javascript 中工作,您需要脚本标签上的
type="module"属性 -
Webstorm 与此无关。它只是一个 IDE。它实际上不会运行代码
-
@AshishGaur 甚至没有接近重复
-
抱歉,没看过
a.js...import {x} from './b.js'
标签: javascript