nuxt-community / moment-module

Efficient Moment.js integration for Nuxt
MIT License
195 stars 12 forks source link

Moment.js Duration Format #1

Closed nzxt closed 5 years ago

nzxt commented 6 years ago

Thanks, great module! My question is next: When i use moment.js as plugin i wrote:

import moment from 'moment'
import momentDurationFormatSetup from 'moment-duration-format'

momentDurationFormatSetup(moment)

export default ({ app }, inject) => {
  inject('moment', moment)
}

How can i extend 'moment.js' with 'moment-duration-format' in your module?

blowsie commented 5 years ago

@nzxt I think i had to solve a similar issue, to use the existing instance you can do the following

import momentDurationFormatSetup from 'moment-duration-format'

export default ({ app }, inject) => {
  momentDurationFormatSetup(app.$moment)
}
ricardogobbosouza commented 5 years ago

@nzxt I think we can close this

kgrosvenor commented 3 years ago

This doesn't work for me, could we reopen? My code also uses locales.

const momentDurationFormatSetup = require("moment-duration-format");

export default ({app}, inject) => {
  const {$moment} = app;
  momentDurationFormatSetup(app.$moment);

  inject('moment', (...args) => {
    const localMoment = $moment(...args);
    localMoment.locale(app.i18n.locale);

    return localMoment;
  });
};
kgrosvenor commented 3 years ago

An update.

This works, i had to set app.$moment!

import moment from 'moment-duration-format'
import momentDurationFormatSetup from 'moment-duration-format'

export default ({app}, inject) => {
  let {$moment} = app;
  momentDurationFormatSetup($moment);

  inject('moment', (...args) => {
    const localMoment = $moment(...args);
    localMoment.locale(app.i18n.locale);

    return localMoment;
  });

  app.$moment = $moment;
};