Home  >  Q&A  >  body text

What are the correct steps to include and use moment in Vuejs?

I'm trying to call moment() in my Vuejs application, I tried declaring it like this:

methods: {
  moment: function () {
    return moment();
  }
},

and try to test it like this:

beforeMount() {
    console.log(moment().format('MMMM Do YYYY, h:mm:ss a'))
},

I keep getting

[Vue warn]: Error in beforeMount hook: "ReferenceError: moment is not defined"

Do I need to add something to package.json and run yarn install?

I want to make it as light as possible because I only need to access moment() one page of my app and I don't want to load them on all pages.

Please exhibitions

P粉403804844P粉403804844213 days ago298

reply all(1)I'll reply

  • P粉252116587

    P粉2521165872024-03-21 00:18:44

    First, you should install it using the following command:

    npm i moment --save

    Then import it in your component and use it like this:

    
    import moment from 'moment/moment';
    
    export default{
    
    ...
    beforeMount() {
        console.log(moment().format('MMMM Do YYYY, h:mm:ss a'))
    }
    
    }
    

    reply
    0
  • Cancelreply