Angular.io(SystemJS)THREE未定义ReferenceError:未定义THREE

时间:2017-10-07 18:08:27

标签: javascript angular typescript three.js

我开始在angular.io中使用three.js并出现此错误:

(index):18 Error: (SystemJS) THREE is not defined
ReferenceError: THREE is not defined
    at Object.eval (http://localhost:3000/node_modules/three/examples/js/controls/OrbitControls.js:16:1)
    at eval (http://localhost:3000/node_modules/three/examples/js/controls/OrbitControls.js:1044:4)
    at eval (http://localhost:3000/node_modules/three/examples/js/controls/OrbitControls.js:1045:3)
    at eval (<anonymous>)
Evaluating http://localhost:3000/node_modules/three/examples/js/controls/OrbitControls.js
Evaluating http://localhost:3000/app/three-basis/three-camera-orbit-control.component.js

我的导入和组件:

import {Component} from '@angular/core';
import {CameraProvider} from './camera-provider.service';
import 'threejs/controls/OrbitControls';
import {OrbitControls} from 'three';

@Component({selector: 'three-camera-orbit-control', template: ''})
export class ThreeCameraOrbitControlComponent {

    constructor(private cameraProvider: CameraProvider) {
        let camera = this.cameraProvider.getCamera();
        let controls = new OrbitControls(camera);
        controls.addEventListener('change', () => {});
    }

}

如果我在我的node_modules / 3中的OrbitControls.js中添加此var THREE = require("three");,则错误gones和all都可以。我还不擅长js,可以请一些人帮助我,如何以正确的方式做到这一点?感谢。

1 个答案:

答案 0 :(得分:-2)

请参阅使用Angular + Three.js的工作示例,包括OrbitControls和ColladaLoader:https://github.com/makimenko/angular-three-examples

目前,Three.js示例未包含在模块中,并且在Angular打字稿中使用它们代码可能有点棘手。解决方案/解决方法之一可能是:

首先,包括依赖项:

three
@types/three

其次,导入组件:

import * as THREE from 'three';
import "./js/EnableThreeExamples";
import "three/examples/js/controls/OrbitControls";
import "three/examples/js/loaders/ColladaLoader";

然后在代码中使用它:

this.controls = new THREE.OrbitControls(this.camera);
this.controls.rotateSpeed = 1.0;
this.controls.zoomSpeed = 1.2;
this.controls.addEventListener('change', this.render);

希望这可以帮助你开始