如何在meteor客户端使用npm模块?
我对如何在Meteor客户端代码中使用npm模块感到困惑。
我理解像fs这样的模块只能在服务器端工作,但在这种情况下,我想使用一个简单的文本模块来显示相当的date:
https://github.com/ecto/node-timeago
我已经尝试在/ public / node_modules下安装模块,它在服务器端很好的遵循这些SO指令:( 我们怎样才能或者可以通过npm使用节点模块和Meteor? )
Meteor.startup(function () { var require = __meteor_bootstrap__.require var timeago = require('timeago') console.log(timeago(new Date())) ...
但是在客户端代码中不起作用:
if (Meteor.is_client) { var require = __meteor_bootstrap__.require var timeago = require('timeago') console.log(timeago(new Date())) ... Uncaught ReferenceError: __meteor_bootstrap__ is not defined"
在这种情况下,服务器端对我来说是无用的,因为我试图在客户端上呈现文本。
我不相信你需要使用服务器端版本。 使用npm的东西只适用于服务器端,顺便说一句,把它放在你的/ public /。 谁知道,也许你可以在你的/ public /中调用它,试试吧。 或者试试这个。
使用类似jquery timeago.js的东西
把它放在/客户端/或类似/客户端/ JS
创build一个/client/helpers.js或者其他的。
使用手柄帮手。
Handlebars.registerHelper('date', function(date) { if(date) { dateObj = new Date(date); return $.timeago(dateObj); } return 'a long long time ago in a galaxy far away'; });
从模板调用'date'句柄辅助函数的示例。
{{ date created }}
handebars助手的date是从meteor/ mongo集合中出来的date。
请参阅github Britto项目。 这是我得到这个代码片段,并在我写的聊天室应用程序中使用它。 工作正常。
还有一些人在那里漂浮。 去madewith.meteor.com并仔细阅读一些项目的来源。