在meteor应用程序中,为什么需要将一些外部脚本放在客户端/子目录中?

时间:2012-11-12 00:25:38

标签: javascript meteor

当我将Mousetrap javascript文件(http://craig.is/killing/mice)放入Meteor应用程序的主目录时,出现以下错误:

Your app is crashing. Here's the latest log.

app/mousetrap.js:711
    _addEvent(document, 'keypress', _handleKey);
              ^
ReferenceError: document is not defined
    at app/mousetrap.js:711:15
    at app/mousetrap.js:813:4
    at /Users/Suchow/Desktop/Collective/.meteor/local/build/server/server.js:107:21
    at Array.forEach (native)
    at Function._.each._.forEach (/Users/Suchow/Desktop/Collective/.meteor/local/build/server/underscore.js:76:11)
    at run (/Users/Suchow/Desktop/Collective/.meteor/local/build/server/server.js:93:7)
Exited with code: 1
Your application is crashing. Waiting for file change.

当Mousetrap文件放在客户端/子目录中时,错误消失。为什么呢?

2 个答案:

答案 0 :(得分:2)

因为服务器端没有dom。 如果你把它放在root中,它已经在服务器端和客户端加载了。

lib像sugarjs,momentjs可以同时工作。

答案 1 :(得分:1)

在Meteor 1.0中,创建包时,强制Meteor仅在客户端加载第三方库的方法是将client参数明确传递给api.addFiles package.js文件

Package.onUse(function(api) {
    api.addFiles('lib/thirdpartylibrary.js', 'client');
}