通过脚本标签在 JavaScript 模块中导入 D3

Ric*_*oln 5 d3.js es6-modules

通过脚本标记在 JavaScript 模块中导入 D3 时,Uncaught TypeError: Cannot read property 'd3' of undefined出现错误。

读到所有主要浏览器现在都支持 JavaScript 模块后,我尝试通过<script type="module" src="network.js"></script>此处描述的标记导入我的 D3 代码:

https://developers.google.com/web/fundamentals/primers/modules

但是,当我导入 D3 时,我收到错误消息:

d3.js:5 Uncaught TypeError: Cannot read property 'd3' of undefined
    at d3.js:5
    at d3.js:6
Run Code Online (Sandbox Code Playgroud)

如果我有一个index.html文件:

d3.js:5 Uncaught TypeError: Cannot read property 'd3' of undefined
    at d3.js:5
    at d3.js:6
Run Code Online (Sandbox Code Playgroud)

与 D3 v5.7.0 在同一目录中,并且network.js只包含:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
</head>
<body>
<script type="module" src="network.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

要不就

import * as d3 from "./d3.js";
Run Code Online (Sandbox Code Playgroud)

收到上述错误。

我希望在 CommonJS 或 AMD 中发现相同的行为:

https://github.com/d3/d3-interpolate/issues/45#issuecomment-329590699