Implementation Method of vue currency filter, vue currency Filter
Custom events can also be used to create custom form input components and use v-model for bidirectional data binding.
To make the v-model of a component take effect, it must:
- Accept a value Attribute
- Triggers input events when new values exist.
The Code is as follows:
HTML:
<div id="app"> <p>{{ message }}</p> <currency-input label="Price" v-model="price"></currency-input> <currency-input label="Shipping" v-model="shipping"></currency-input> <currency-input label="Handling" v-model="handling"></currency-input> <currency-input label="Discount" v-model="discount"></currency-input> <p>Total: ${{ total }}</p></div>
Javascript:
Vue.component('currency-input', { template: `\ <div>\ <label v-if="label">{{ label }}</label>\ $\ <input\ ref="input"\ v-bind:value="value"\ v-on:input="updateValue($event.target.value)"\ v-on:focus="selectAll"\ v-on:blur="formatValue"\ >\ </div>\ `, props: { value: { type: Number, default: 0 }, label: { type: String, default: '' } }, mounted: function () { this.formatValue() }, methods: { updateValue: function (value) { var result = currencyValidator.parse(value, this.value) if (result.warning) { this.$refs.input.value = result.value } this.$emit('input', result.value) }, formatValue: function () { this.$refs.input.value = currencyValidator.format(this.value) }, selectAll: function (event) { setTimeout(function () { event.target.select() }, 0) } }})new Vue({ el: '#app', data: { message: 'Hello Vue.js!', price: 0, shipping: 0, handling: 0, discount: 0 }, computed: { total: function () { return (( this.price * 100 + this.shipping * 100 + this.handling * 100 - this.discount * 10 ) / 100).toFixed(2) } }})
As follows:
Each Vue instance implements the Events interface, namely:
Use $ on (eventName) to listen for events
Use $ emit (eventName) to trigger an event
V-model implements bidirectional transmission.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.