【问题标题】:Why am I getting an "Uncaught ReferenceError: THREE is not defined" error from three.js even when I have added a requirejs shim为什么即使我添加了 requirejs shim,我也会从 three.js 收到“未捕获的 ReferenceError:未定义三个”错误
【发布时间】:2017-06-01 19:52:50
【问题描述】:

运行我的代码时出现以下错误: 未捕获的 ReferenceError:未定义三个

module game {
    export class Add3DScene extends dragonwings.Command {

        @inject('ResponsiveDiv')     protected _responsiveDiv:          components.ResponsiveDiv;

        public execute(): void {
            super.execute();
            var scene = new THREE.Scene();
        }
    }
}

我无法尝试this 解决方案,因为我无权访问嵌入的 html

然而,我尝试过 this,但添加 shim 似乎没有帮助,而且由于不了解如何将其合并到我的打字稿中,我无法添加定义包装器。

我可以看到在 chrome 调试器中加载了three.min.js

我正在使用 requirejs,这似乎会导致我在网上找到的一些复杂情况。这是我的配置的简化版本

requirejs.config({
    baseUrl: "content/${gamecode}/lib",
    paths: {
        TweenMax:           "gsap/TweenMax",
        THREE:              "three/three.min",
    },
    shim:
    {
        'THREE': {
            exports: 'THREE'
        },
        'game_libs': {
            deps: ['stats', 'TweenMax']
        },
        '../app/js/main': {
            deps: ['game_libs']
        },
    }
});

任何帮助表示赞赏

**编辑 在查看Require.js not loading Three.js 之后,我的其他模块不再定义,所以我还没有开始执行我最初发布的代码 新的需要配置

define("three-glue", ["three"], function (three) {
    window.THREE = three;
    return three;
});
requirejs.config({
    baseUrl: "content/${gamecode}/lib",
    waitSeconds: 60,
    paths: {
        TweenMax:           "gsap/TweenMax",
        three:            "three/three.min",
    },
    map: {
        '*': {
            three: 'three-glue'
        },
    'three-glue': {
        three: 'three'
    },
    shim:
    {
        'game_libs': {
            deps: [
                'TweenMax'
            ]
        },
        '../app/js/main': {
            deps: ['game_libs']
        },
    }
}
});

【问题讨论】:

  • 你有本地的库代码吗?
  • 是的,我已经手动将 three.min.js 复制到项目中(加载成功)并通过 npm install --save @types/three 包含我的定义文件
  • 你是否使用了three.js 映射文件以便打字稿能够识别它?
  • 您需要在生成的模块的 require 调用中导入需要注入的模块。尝试添加 import 'THREE';在你的模块顶部

标签: typescript three.js requirejs


【解决方案1】:

所以我添加地图的位置很重要,如果它是在路径之后但在垫片之前添加的,那么我会丢失所有其他模块。如果我在 shims 之后添加它,那么只有三个模块仍然丢失。

这是因为我忘记添加三个作为依赖项。更正我的要求配置后,我不再收到任何错误。

'../app/js/main': {
            deps: ['game_libs', 'three']
        },

感谢您的帮助

【讨论】:

  • map 相对于shim 的顺序无关紧要。这不能重要。这是基本的 JavaScript。 {a: 1, b: 2}{b: 2, a: 1}` 包含相同的信息。 ab 的顺序无关紧要。
  • 我同意,但确实如此
  • 您以其他方式向自己的脚开了一枪,并将其归因于对象属性的顺序。把东西放在错误的地方是非常容易的(例如mappaths 中)并且RequireJS 不会报告错误。它将无法按照您的预期运行。
猜你喜欢
  • 2017-04-26
  • 2016-09-10
  • 2012-02-01
  • 2016-09-15
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-30
相关资源
最近更新 更多