[英]Errors : Cannot use import statement outside a module THREE.OrbitControl
Im starting with three.js.我从 three.js 开始。 After trying to implement orbit controls i have some errors.
在尝试实施轨道控制后,我遇到了一些错误。 It looks simple but i cannot find a good solution for my errors.
它看起来很简单,但我无法为我的错误找到一个好的解决方案。 When im trying to implement controls like:
当我尝试实现以下控制时:
var controls = new THREE.OrbitControls(camera, renderer.domElement);
im getting these errors我收到这些错误
Cannot use import statement outside a module and THREE.OrbitControls is not a constructor不能在模块外使用 import 语句并且 THREE.OrbitControls 不是构造函数
I added both threejs and orbitcontrols just before starting a new script.在开始一个新脚本之前,我添加了threejs 和 orbitcontrols。 What am I doing wrong here?
我在这里做错了什么?
<script src="scripts/three.js"></script>
<script src="scripts/OrbitControls.js"></script>
<script type="text/javascript">
var scene = new THREE.Scene();
var camera = new
THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x888888,1)
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
It seems your version of OrbitControls.js
is a ES6 module.看来您的
OrbitControls.js
版本是 ES6 模块。 That means you have to import the controls like so:这意味着您必须像这样导入控件:
<script type="module">
import * as THREE from 'scripts/three.module.js';
import { OrbitControls } from 'scripts/OrbitControls.js';
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x888888,1)
document.body.appendChild(renderer.domElement);
var controls = new OrbitControls(camera, renderer.domElement);
Notice that the above code uses the three.module.js build file which you can find in the build directory of the official git repository.请注意,上面的代码使用了three.module.js构建文件,您可以在官方 git 存储库的构建目录中找到该构建文件。 If you are not familiar with ES6 modules, I suggest you study an existing resource about this topic.
如果您不熟悉 ES6 模块,我建议您研究有关此主题的现有资源。 Eg https://exploringjs.com/es6/ch_modules.html
例如https://exploringjs.com/es6/ch_modules.html
three.js R109
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.