我创建了一个基本的 Meteor 项目,带有一个基本包。在包中,我想使用 _.extend()
。即使在我显式使用 meteor add underscore
之后,这也会失败并出现错误:
Uncaught ReferenceError: _ is not defined
这是我的步骤:
$ meteor create UnderScore
$ cd UnderScore/
$ meteor add underscore
$ meteor create --package unscore
$ meteor add unscore
在文件 UnderScore/packages/unscore/unscore.js
中,我添加了代码
if (Meteor.isClient) {
var object = {}
_.extend(object, {key: value}) // ERROR OCCURS ON THIS LINE
}
我现在使用 meteor run
启动应用程序,并在 http://localhost:3000 打开浏览器
我立即在控制台中收到错误报告:
Uncaught ReferenceError: _ is not defined unscore.js:3
我可以在不在包内的 JavaScript 文件中毫无问题地访问 _
。使 _
在包内可见的正确巫术是什么?
最佳答案
为了让您的包使用下划线,需要将其添加为依赖项。你的 packages/unscore/package.js
应该看起来像这样:
Package.onUse(function(api) {
api.use('underscore');
api.addFiles('unscore.js');
});
关于javascript - _ 在 Meteor 包中使用时无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32406625/