Summer Sale! All accounts are 50% off this week.

mesqueeb's avatar

How to add a custom Vue Filter to v-model in vue2?

In Vue 1 I did:

v-model="item.planned_time | min_to_hours"

In Vue 2 this is not possible... But I'm not sure how to combine the two.

This is my custom filter:

Vue.filter('min_to_hours', {
  // model -> view
  // formats the value when updating the input element.
  read: function(val) {
    let nr = val/60;
    if (val == 10){ nr = nr.toFixed(2); }
    return nr;
  },
  // view -> model
  // formats the value when writing to the data.
  write: function(val, oldVal) {
    let nr = parseFloat(val)*60;
    return nr;
  }
});
0 likes
1 reply

Please or to participate in this conversation.