我对如何在Meteor客户端代码中使用npm模块感到困惑.
我理解像fs这样的模块只能在服务器端工作,但在这种情况下,我想使用这样一个简单的文本模块来显示漂亮的日期:
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()))
...
Run Code Online (Sandbox Code Playgroud)
但是它在客户端代码中不起作用:
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"
Run Code Online (Sandbox Code Playgroud)
在这种情况下,服务器端对我来说没用,因为我正在尝试在客户端上呈现文本.
我不相信你需要使用服务器端版本.仅将npm内容用于服务器端,顺便提一下,也可以将它放在/ public /中.谁知道也许你可以在你的/ public /中调用它,试试吧.或试试这个.
使用像jquery timeago.js这样的东西
把它放在/ client /或/ client/js之类
创建一个/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';
});
Run Code Online (Sandbox Code Playgroud)
从模板调用'date'把手辅助函数的示例.
{{ date created }}
Run Code Online (Sandbox Code Playgroud)
其中date是handebars helper并且创建的是来自meteor/mongo集合的日期.
查看github Britto项目.这就是我得到这段代码片段并在我写的聊天室应用中使用它的地方.工作良好.
还有其他几个漂浮在那里.访问madewith.meteor.com并仔细阅读一些项目的来源.
归档时间: |
|
查看次数: |
3328 次 |
最近记录: |