vue货币过滤器的实现方法
作者:CURRY_zhao 时间:2024-05-09 10:40:58
自定义事件也可以用来创建自定义的表单输入组件,使用 v-model 来进行数据双向绑定。
所以要让组件的 v-model 生效,它必须:
接受一个 value 属性
在有新的 value 时触发 input 事件
代码如下:
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)
}
}
})
效果图如下:
每个 Vue 实例都实现了事件接口(Events interface),即:
使用 $on(eventName) 监听事件
使用 $emit(eventName) 触发事件
v-model实现双向传递。
标签:vue,过滤器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python模块结构与布局操作方法实例分析
2021-11-26 10:49:31
Python 在字符串中加入变量的实例讲解
2023-01-27 10:51:21
详解Python爬虫爬取博客园问题列表所有的问题
2021-02-15 02:01:46
![](https://img.aspxhome.com/file/2023/8/109658_0s.jpg)
BootStrap的select2既可以查询又可以输入的实现代码
2024-04-28 10:18:41
python 详解如何使用GPU大幅提高效率
2023-08-24 19:45:46
![](https://img.aspxhome.com/file/2023/0/62550_0s.png)
ASP使用wsImage组件给图片加水印代码
2010-06-09 19:23:00
PHP实现的服务器一致性hash分布算法示例
2024-06-05 09:49:25
PHP获取指定日期是星期几的实现方法
2024-05-09 14:47:21
Python argparse库的基本使用步骤
2023-12-14 08:02:29
MySQL中与NULL值有关的疑难问题
2008-11-24 12:47:00
go类型转换及与C的类型转换方式
2024-04-28 09:18:38
k8s在go语言中的使用及client 初始化简介
2024-02-03 18:10:51
为ABP框架增加日志组件与依赖注入服务
2024-06-05 15:43:32
php函数连续调用实例分析
2024-03-17 06:48:38
JS中的forEach、$.each、map方法推荐
2024-04-29 13:19:59
Python实现数字的格式化输出
2021-10-11 18:11:27
一直闪烁变色的超级链接代码
2008-02-27 13:08:00
微信小程序仿朋友圈发布动态功能
2024-04-17 10:01:01
![](https://img.aspxhome.com/file/2023/6/136426_0s.png)
pandas数据清洗,排序,索引设置,数据选取方法
2023-06-13 15:08:45
PyTorch上搭建简单神经网络实现回归和分类的示例
2022-08-02 04:49:42
![](https://img.aspxhome.com/file/2023/2/94162_0s.jpg)