Vue.js and vue-moment: “Failed to resolve filter: moment”

后端 未结 4 1296
时光说笑
时光说笑 2021-02-10 09:06

Trying to use vue-moment. The simplest possible example from the documentation doesn\'t work: https://jsfiddle.net/rjcpz9wt/

{{ new Date() | moment \         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-10 09:33

    /////EDIT/////

    apparently you don't need to put moment inside a Vue.filter(....) at all.

    after you entered Vue.use(VueMoment) in main.js, simply put a filter as shown in their documentation anywhere in your app and it just works

    //////////////

    I encountered the same problem, solved it by not using vue filters

    this is my workaround, my main.js looks like this:

    import Vue from 'vue'
    import VueMoment from 'vue-moment';
    
    Vue.use(VueMoment)

    and in the component wherever i need to use moment it looks like this:

    //IN METHODS:
    methods: {
        tellTime(time) {
          console.log(this.$moment(time).format(' mm:ss'))
        }
    }
    
          {{$moment(timestamp).fromNow()}}

    Hope someone else would find it useful :)

提交回复
热议问题