Using three.js in Angular2

久未见 提交于 2019-12-07 07:05:33

问题


I am trying to integrate a three.js scene into a angular 2 page. I am a beginner in Angular 2 and javascript, I have included the three.js file in the script tag, in index.html, i.e, and in the scene.component.ts file I have the code like this...

//scene.component.ts
import { Component } from 'angular2/core';
import { THREE } from 'three-js/three';
@Component({
    selector: 'ps-scene',
    templateUrl: 'app/webgl/scene.component.html'
})
export class SceneComponent{
    scene = new THREE.Scene();
}
///////////////

The problem is showing in the line - import { THREE } from 'three-js/three';

This is in the same directory path as 'angular2/core'

Thanks in advance.


回答1:


are you using angular cli?

if so, don't forget to add the reference in your typings.d file:

declare module 'three';

https://github.com/angular/angular-cli#3rd-party-library-installation

3rd Party Library Installation

Simply install your library via npm install lib-name --save and import it in your code.

If the library does not include typings, you can install them using npm:

npm install d3 --save
npm install @types/d3 --save-dev
If the library doesn't have typings available at @types/, you can still use it by manually adding typings for it:

// in src/typings.d.ts
declare module 'typeless-package';

// in src/app/app.component.ts
import * as typelessPackage from 'typeless-package';
typelessPackage.method();



回答2:


Checkout this ng2-three-demo, where I use Angular Components to define objects for ThreeJS.

Additionally, there is a presentation on the topic itself in the repo.




回答3:


It probably should be imported like this:

import * as THREE from 'three';



回答4:


I'm getting started on Angular 2 specifics about integrating other libraries to your own projects, so my guess is that it'd have to be included in your system.js config folder.

What I really want to show you is this angular 2 + three.js implementation to give you some light on the subject.

I would post it as a comment, but I don't have the reputation yet to do so.

Cheers!



来源:https://stackoverflow.com/questions/40085132/using-three-js-in-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!