Answer the question
In order to leave comments, you need to log in
How to work with moment.js inside vue.js?
Hey!
I am making a very simple script with dates, I wanted to do it as soon as possible without using webpack, I just added vue (because I need reactivity) and added moment. Moment works fine without vue. But inside vue itself, it says that the function is undefined. Okay, I understand what vue is looking for in its local area, and I found such a solution. In methods add:
moment: function () {
return moment();
}
var moment = moment();
var app = new Vue({
el: '#wrapper',
data: { },
methods: {
makeCalculate: function () {
var today = moment.format('DD.MM.YYYY'); //отлично работает
},
moment: function () {
return moment();
}
}
});
Answer the question
In order to leave comments, you need to log in
var moment = moment();
The problem is this. At the moment when you write var moment
- the global variable in this context is forgotten and the local one appears. Accordingly, when you do = moment()
- it does not refer to the global variable that the library created, but to the local one that you just created.
Just remove this line and everything should work:var moment = moment();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question