javascript - 无法使用 Three.js 导入 OrbitControls.js

标签 javascript

我正在浏览three.js,并找到了这个例子。
https://threejsfundamentals.org/threejs/lessons/threejs-load-gltf.html
不幸的是,当我使用 Flask 在本地运行它时,我不断收到所有三个导入的错误。

import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r122/build/three.module.js';
import {OrbitControls} from 'https://threejsfundamentals.org/threejs/resources/threejs/r122/examples/jsm/controls/OrbitControls.js';
import {GLTFLoader} from 'https://threejsfundamentals.org/threejs/resources/threejs/r122/examples/jsm/loaders/GLTFLoader.js';
Uncaught SyntaxError: Cannot use import statement outside a module我一直在寻找解决方案的高低,但找不到任何解决方案。
我还尝试使用脚本标签运行三个导入。
<script src="{{url_for('static',filename='js/three/build/three.js')}}"></script>
<script type="module" src="{{url_for('static',filename='js/three/examples/jsm/controls/OrbitControls.js')}}"></script>
<script type="module" src="{{url_for('static',filename='js/three/examples/jsm/loaders/GLTFLoader.js')}}"></script>
这可行,但是当我尝试下面的代码时,我得到:Uncaught ReferenceError: OrbitControls is not defined
const controls = new OrbitControls(camera, canvas);
controls.target.set(0, 5, 0);
controls.update();
此外,如果我尝试使用 GLTFLoader,我会收到错误消息。
const loader = new THREE.GLTFLoader();
或者
const loader = new GLTFLoader();
任何想法我如何解决这个问题将不胜感激。

最佳答案

您需要说明您的脚本标签的类型为 module .所以你需要的是这样的:

<html>
  <body>
    <script type="module">
      import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r122/build/three.module.js';
      import {OrbitControls} from 'https://threejsfundamentals.org/threejs/resources/threejs/r122/examples/jsm/controls/OrbitControls.js';
      import {GLTFLoader} from 'https://threejsfundamentals.org/threejs/resources/threejs/r122/examples/jsm/loaders/GLTFLoader.js';
    </script>
  </body>
</html>

关于javascript - 无法使用 Three.js 导入 OrbitControls.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65864573/

相关文章:

javascript - 为什么 ("foo"=== new String ("foo")) 在 JavaScript 中计算为 false?

javascript - 根据数组搜索表格单元格文本

javascript - 流和事件循环 - 数据处理程序是否总是在下一个时钟周期触发?

javascript - 如何在查询中从表中获取属性?

javascript - 更改配置后通过WebView和JS函数调用JavascriptInterface方法经常不起作用

javascript - 如何获取和设置 ES6 Maps 中的数组键?

javascript - 如何在三星 galaxy S2 中查看控制台日志输出

Javascript 函数仅适用于表格的第一行

javascript - JQuery:对 .append 元素的高度进行动画处理

javascript - 在 selectize.js 中停止传播