Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I import @types/three in angular2

After several hours search and test. I finally give up. I'm using Angular2 with webpack, I try to use three.js in my angular2 app. I have installed the npm package @type/three

sudo npm install @types/three --save

And I have edited my tsconfig.json in multiple ways. I even tried to add import "three/three" in my polyfills.browser.ts. But I keep getting con't resolve module error. Maybe there is something wrong with my tsconfig.json as following

    {
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "outDir": "dist",
        "rootDir": ".",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "moduleResolution": "node",
        "typeRoots": [
            "./node_modules/@types"
        ],
        "types": [
            "core-js",
            "node",
            "three"
        ]
    },
    "exclude": [
        "node_modules"
    ],
    "awesomeTypescriptLoaderOptions": {
        "useWebpackText": true
    },
    "compileOnSave": false,
    "buildOnSave": false,
    "atom": {
        "rewriteTsconfig": false
    }
}

and I have tried at least the following syntax in my Component

import {THREE} from "@types/three";
import {THREE} from "three";
import "@types/three";
import "three";
import * as _ from "@types/three";
import * as _ from "three";

Actually I don't really understand how all those tsconfig, webpackconfig work, so when I try to implement this @types/module I have no idea what I'm doing. Any help would be appreciate, Thanks!

like image 919
YugoAmaryl Avatar asked Dec 13 '16 09:12

YugoAmaryl


People also ask

What is import and export in angular?

An import is what you put in the imports property of the @NgModule decorator. It enables an Angular module to use functionality that was defined in another Angular module. An export what you put is the exports property of the @NgModule decorator.


1 Answers

You have to install three.js package as well.

npm install three --save
npm install @types/three --save

A testing component:

import { Component, AfterViewInit } from '@angular/core';
import * as THREE from 'three';

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>`,
})
export class AppComponent implements AfterViewInit {
  name = 'Angular';
  scene: any;
  camera: any;
  renderer: any;
  geometry: any;
  material: any;
  mesh: any;

  ngAfterViewInit() {
    this.init();
    this.animate();
  }

  init() {
    this.scene = new THREE.Scene();

    this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
    this.camera.position.z = 1000;

    this.geometry = new THREE.BoxGeometry(200, 200, 200);
    this.material = new THREE.MeshBasicMaterial({ color: 0xff0000, wireframe: true });

    this.mesh = new THREE.Mesh(this.geometry, this.material);
    this.scene.add(this.mesh);

    this.renderer = new THREE.WebGLRenderer();
    this.renderer.setSize(window.innerWidth, window.innerHeight);

    document.body.appendChild(this.renderer.domElement);

  }

  animate() {
    requestAnimationFrame(this.animate);
    this.mesh.rotation.x += 0.01;
    this.mesh.rotation.y += 0.02;
    this.renderer.render(this.scene, this.camera);
  }
}

add map in systemjs.config.js

'three': 'npm:/three/build/three.min.js',
like image 174
Yong Avatar answered Sep 23 '22 08:09

Yong