简体   繁体   English

错误:不能在模块外使用 import 语句 THREE.OrbitControl

[英]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.

相关问题 在THREE.OrbitControl中的phi与哪个轴成角度? - phi in THREE.OrbitControl is an angle with which axis? Stackblitz:不能在模块外使用 import 语句 - Stackblitz: Cannot use import statement outside a module 不能在模块外使用 import 语句@emotion - Cannot use import statement outside a module @emotion 尝试使用 three.js 时出现“未捕获的语法错误:无法在模块外使用 import 语句” - “Uncaught SyntaxError: Cannot use import statement outside a module” when trying to use three.js Javascript - 不能在模块外使用导入语句 - Javascript - Cannot use import statement outside a module firebase中的模块外不能使用import语句 - Cannot use import statement outside the module in firebase 不能在模块外使用 import 语句 - Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module 导入模块时不能在模块外使用 import 语句 - Cannot use import statement outside a module when I import a module 语法错误:无法在模块外部使用 import 语句/意外标记“&lt;” - 相关错误 - Syntax Error: Cannot use import statement outside a module / Unexpected token '<' - related errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM