我如何在angular2中导入@ types/three [英] How could I import @types/three in angular2

查看:189
本文介绍了我如何在angular2中导入@ types/three的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时后进行搜索和测试.我终于放弃了.我正在将Angular2与webpack一起使用,我尝试在我的angular2应用程序中使用three.js.我已经安装了npm软件包@ type/three

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

并且我已经以多种方式编辑了tsconfig.json.我什至尝试在polyfills.browser.ts中添加导入三/三".但是我一直无法解决模块错误.我的tsconfig.json可能有问题,如下所示

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";

实际上,我不太了解所有这些tsconfig,webpackconfig的工作原理,因此,当我尝试实现此@ types/module时,我不知道我在做什么.任何帮助将不胜感激,谢谢!

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!

推荐答案

您还必须安装three.js软件包.

You have to install three.js package as well.

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

一个测试组件:

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);
  }
}

在systemjs.config.js中添加地图

add map in systemjs.config.js

'three': 'npm:/three/build/three.min.js',

这篇关于我如何在angular2中导入@ types/three的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆