What are the correct steps to include and use moment in Vuejs?
P粉403804844
P粉403804844 2024-03-20 09:44:12
0
1
334

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粉403804844
P粉403804844

reply all(1)
P粉252116587

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'))
}

}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template