我如何加载特定模板的three.js

How can I load three.js for specific templates

本文关键字:js three 何加载 加载      更新时间:2023-09-26

我知道有两种将外部库加载到meteor的主要方法(如http://www.manuel-schoebel.com/blog/use-meteor-iron-router-waiton-to-load-external-javascript中所讨论的)。

此外,还有流星包全局加载three.js

有人知道如何使用模板级方法来获得THREE工作吗?我不希望three.js加载所有的路线,但我的尝试到目前为止使用~

Template.threeview.onRendered(function(){
    Session.setDefault('threejs', false);
    // is library already loaded?
    if (Session.get('threejs') === false) {
        console.log('Requesting threejs...')
        $.getScript('/js/three.min.js', function() {
            Session.set('threejs', true);
            // the THREE object should now be accessible here
            // as well as in the window environment...?
            console.log(window.THREE);
            console.log(THREE);
        });
    };
});

不工作-我得到

undefined 
Uncaught ReferenceError: THREE is not defined

在js控制台窗口。

任何想法?

编辑

发现这个类似的问题可以'找不到3 .js通过Ajax加载后遭受同样的问题-我可以确认从CDN加载工作。有人有更好的解决办法吗?

最后弄明白了——让这个工作的方法是编辑three.min.js源文件。在three.min.js的开头删除/注释'use strict';可以让它正确加载。

这似乎与以下流星的特性/问题有关:https://github.com/meteor/meteor/issues/1380

CDN未缩小且不包含'use strict;'。为了安全起见,'use strict;'应该放在全局THREE命名空间声明之后。