What's the proper way to implement formatting on v-model in Vue.js 2.0

前端 未结 4 1476
耶瑟儿~
耶瑟儿~ 2021-02-05 02:45

For a simple example: textbox to input currency data. The requirement is to display user input in \"$1,234,567\" format and remove decimal point.

I have tried vue direct

4条回答
  •  难免孤独
    2021-02-05 03:24

    I implemented a component. According to Mani's answer, it should use $emit.

    Vue.component('currency', {
    template: '',
    props: ['placeholder', 'title', 'value'],
    computed: {
        formatted: {
            get: function () {
                var value = this.value;
                var formatted = currencyFilter(value, "", 0);
                return formatted;
            },
            set: function (newValue) {
                var cleanValue = newValue.replace(",", "");
                var intValue = parseInt(cleanValue, 10);
                this.value = 0;
                this.value = intValue;
            }
        }
    }
    }
    

    );

提交回复
热议问题