我正在尝试OrbitControls
在我的项目中使用 THREE.js 。当我尝试导入该OrbitControls
对象时,我在 Google Chrome DevTools 控制台中收到以下错误消息。
请求的模块“./ Three.js”不提供名为“EventDispatcher”的导出
当我手动检查three.js
(r119) 时,我可以看到它确实在第 50631 行导出EventDispatcher
。
问题:鉴于此信息,为什么我会收到上述错误消息,以及如何解决此问题?
<!DOCTYPE html>
<html lang="en">
<body>
<script src="three.js" crossorigin="anonymous"></script>
<script src="main.js" type="module"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
import { OrbitControls } from './OrbitControls.js'
Run Code Online (Sandbox Code Playgroud)
编辑:感谢 Steve,我的问题的根本原因是我使用的是非模块版本,three.js
而不是正确的版本(对于我的用例)three.module.js
。如果您收到错误消息,请确保您正在下载该three.module.js
文件。
您将模块库与非模块库混合在一起。如果你看第一行OrbitControls.js
import {
EventDispatcher,
MOUSE,
Quaternion,
Spherical,
TOUCH,
Vector2,
Vector3
} from "../../../build/three.module.js";
Run Code Online (Sandbox Code Playgroud)
它正在尝试从 导入变量../../../build/three.module.js
,该变量可能不存在。从您的错误消息来看,似乎有人已经将其编辑为“./ Three.js”,这听起来像是非模块版本。
three.js
到一个文件夹(例如lib/three
)通过以下方式导入:
import { OrbitControls } from './lib/three/examples/jsm/controls/OrbitControls.js';
import * as THREE from './lib/three/build/three.module.js';
Run Code Online (Sandbox Code Playgroud)
或者您可以仅下载您需要的文件并更改参考:
three.module.js
从https://github.com/mrdoob/ Three.js/blob/dev/build/third.module.js下载../../../build/three.module.js
到OrbitControls.js
适当的路径。它应该是three.module.js
与OrbitControls.js
<script src="three.js">
HTML 中的标签,因为OrbitControls.js
直接导入它或者,您可以直接链接到 CDN,例如:
import { OrbitControls } from 'https://unpkg.com/three@0.119.1/examples/jsm/controls/OrbitControls.js';
import * as THREE from 'https://unpkg.com/three@0.119.1/build/three.module.js';
Run Code Online (Sandbox Code Playgroud)
这是可行的,因为所有必需的文件都由 CDN 托管