In Vue JS, call a filter from a method inside the vue instance

后端 未结 5 1698
自闭症患者
自闭症患者 2020-12-23 14:24

Say I have a Vue instance like so:

new Vue({
    el: \'#app\',

    data: {
        word: \'foo\',
    },

    filters: {
       capitalize: function(text) {         


        
5条回答
  •  误落风尘
    2020-12-23 14:58

    To complement Morris answer, this is an example of a file I normally use to put filters inside, you can use in any view using this method.

    var Vue = window.Vue
    var moment = window.moment
    
    Vue.filter('fecha', value => {
      return moment.utc(value).local().format('DD MMM YY h:mm A')
    })
    
    Vue.filter('ago', value => {
      return moment.utc(value).local().fromNow()
    })
    
    Vue.filter('number', value => {
      const val = (value / 1).toFixed(2).replace('.', ',')
      return val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.')
    })
    Vue.filter('size', value => {
      const val = (value / 1).toFixed(0).replace('.', ',')
      return val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.')
    })
    

提交回复
热议问题