1
  • 我正在尝试使用 moment(...).fromNow() 来显示相对时间
  • 我正在使用@nuxtjs/moment模块
  • 它目前将相对时间显示为“15 天前”等。我想将其更改为“15 d”

根据这个答案是目前如何完成

moment.updateLocale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s: function (number, withoutSuffix, key, isFuture){
            return '00:' + (number<10 ? '0':'') + number + ' minutes';
        },
        m:  "01:00 minutes",
        mm: function (number, withoutSuffix, key, isFuture){
            return (number<10 ? '0':'') + number + ':00' + ' minutes';
        },
        h:  "an hour",
        hh: "%d hours",
        d:  "a day",
        dd: "%d days",
        M:  "a month",
        MM: "%d months",
        y:  "a year",
        yy: "%d years"
    }
});

如何将其应用于 @nuxtjs/moment 模块

4

1 回答 1

1

制作一个 nuxt 插件并使用上下文访问 $moment

export default (context) => {
  context.$moment.updateLocale('en', {
    relativeTime: {
      future: '%s',
      past: '%s',
      s: '1 s',
      ss: '%d seconds',
      m: '1 m',
      mm: '%d m',
      h: '1 h',
      hh: '%d h',
      d: '1 d',
      dd: '%d d',
      M: '1 M',
      MM: '%d M',
      y: '1 Y',
      yy: '%d Y',
    },
  })
}
于 2020-09-02T05:42:28.063 回答