简体   繁体   中英

Call a method from the Vue.js component

I'm newbie to Vue.js

I just want to call a method from a component:

var app = new Vue({
    el: '#app',
    components: {
        message: {
            props: ['createdat'],
            template: '
            <div>
              {{ postedOnA(createdat) }}
              {{ postedOnB(createdat) }}
            </div>',
        },
        methods: {
            postedOnA: function (createdat) {
                var date = new Date(createdat);
                return date.getHours() + ' ' + date.getMinutes();
            }
        }
    },
    methods: {
        postedOnB: function(createdat) {
            var date = new Date(createdat);
            return date.getHours() + ' ' + date.getMinutes();
        }
    }
})

I have also tried to call a component method, but that is not working too

Thanks

Create filters for formatting.

In your case:

var app = new Vue({
    el: '#app',
    components: {
        message: {
            props: ['createdat'],
            template: '
            <div>
              {{ createdat | formatdate }}
            </div>',
        }
    },
    filters: {
      formatdate: function (value) {
         var date = new Date(value);
         return date.getHours() + ' ' + date.getMinutes();
      }
    }
})

Read more here: https://v2.vuejs.org/v2/guide/filters.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM