meteor - Momentjs 不更新时间 .fromNow()

标签 meteor

当我加载页面时一切正常,但是:为什么 momentjs 不更新时间?
如果我插入一个新帖子,momentjs 不会用一分钟前更新几秒钟前的文本。

这就是我正在做的事情。

我添加了 momentjs:moment包裹

注册助手

UI.registerHelper('timeAgo', function(datetime) {
    return moment(datetime).fromNow();
});

在模板中

<template name="postItem">
    <li>
      {{text}} - {{timeAgo createdAt}}
    </li>
  {{/if}}
</template>

输出是

Hi! - a few seconds ago
Hello! - 23 minutes ago    

最佳答案

日期不是 react 变量,因此当时差变化时它不会刷新。

您可以通过强制它每分钟(或自定义间隔)重新计算来使其具有反应性:

UI.registerHelper('timeAgo', function(datetime) {
    Session.get('time');
    return moment(datetime).fromNow();
});

setInterval(function() {
    Session.set("time", new Date())
}, 60000); //Every minute

这将强制助手每分钟重新绘制时间值。

关于meteor - Momentjs 不更新时间 .fromNow(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677470/

相关文章:

javascript - float 侧边栏语义 Ui

Meteor JS 帐户-google 包对我不起作用

javascript - 从包访问 meteor 应用程序的导入目录?

facebook - 在 MeteorJS 框架中运行 facebook 身份验证?

meteor - `mrt add` 在 Windows 上使用 Vagrant 安装 Atmosphere 包时引发未知错误

javascript - 将 `this.data` 的上下文转移到 Blaze/Meteor 中的其他模板?

javascript - meteor :TypeError:Object #<Object> 没有方法 'require'

javascript - Meteor - 从 Lib 文件夹访问方法

javascript - 在 Meteor.user 字段中保存了对象的 id,在模板中调用了 id,如何显示对象的数据而不是 id?

javascript - 插入/更新到具有未确认写入问题的 Meteor 集合