How to put momentsjs in a VueJS application


I want to put the momentJS library in my Vue application, and that the full functionality of moment is available in any component of my app.

asked by jBaumann 27.07.2018 в 10:00

1 answer


To 'install' moment.js in your Vue app we do the following:

// main.js
import Vue from 'vue'
import moment from 'moment'
import App from './App'

Object.defineProperty(Vue.prototype, '$moment', {value: moment})

With this we already have access to momentum in our components in this way: this.$moment()

Example, in our App.vue file

const date = "21/01/2018"
const format = "DD/MM/YYYY"
this.$moment(date, format).isValid()

Edit :
As Pablo says in the comment below, this can be used with other libraries, for example lodash

answered by 27.07.2018 / 10:07