带有TypeScript的Three.js STL加载器 - 未捕获TypeError:无法读取属性' XHRLoader'未定义的

时间:2017-01-06 15:33:21

标签: typescript three.js

我正在尝试将STL文件导入Three.js,但three-stl-loader npm-module没有找到THREE对象。

此代码:

import * as THREE from 'three'
import * as THREESTLLoader from 'three-stl-loader'

var STLLoader = new THREESTLLoader()
var loader = new STLLoader(THREE)
loader.load('path/to/file.stl', function (geometry: any) {})

产生此错误: enter image description here

在index.js的第45行(在stl-loader中),three-stl-loader模块使用THREE对象,但似乎模块不知道{{1对象根本: enter image description here

该项目是使用Webpack中的TypeScript编译的。我认为它的编译方式可能存在问题。

1 个答案:

答案 0 :(得分:2)

似乎我找到了缺失的部分。 由于模块而不是构造函数必须接收THREE对象,我只需要将THREE添加到模块中:

import * as THREE from 'three'
import * as THREESTLLoader from 'three-stl-loader'

var STLLoader = new THREESTLLoader(THREE) // Added THREE
var loader = new STLLoader() // Removed THREE
loader.load('path/to/file.stl', function (geometry: any) {})