VUE Moment.js does not update the date in real time

0

I'm starting to use the Moments library of VUE.JS, I have a message area on my website and I need the date to be shown as: "Posted 5 minutes ago", I was able to show the date with the code below, but I noticed that the same only refresh refreshes, is not reactive, what can I have done wrong?

<script src="https://momentjs.com/downloads/moment.js"></script><divid="app-ask-marketplace">
    <div>{{moment(item.date, "YYYY-MM-DD hh:ii").fromNow()}}</div>
</div>



<script>
    new Vue({
        el:"#app-ask-marketplace",
        data : {

        },

        methods : {


        },  mounted(){ 

        }

    })
</script>
    
asked by anonymous 07.12.2018 / 12:12

1 answer

0

Within the template "global" variables within the module are not accessible, if you want to use it, you have to associate with the instance, for example in data .

But in your case you can do this with a computed , or with a filter if the item is being created within a v-for :

<div>{{item.date | yymmdd}}</div>

And in JS:

filter: {
    yymmdd(date ){
       return moment(date, "YYYY-MM-DD hh:ii").fromNow();
    }

Or as I explained above:

No JS:

import moment from 'moment';

...
  data: {
    moment: moment

And in the template:

<div>{{moment(item.date, "YYYY-MM-DD hh:ii").fromNow()}}</div>
    
09.12.2018 / 20:45