Vue数字输入框组件示例代码详解
前端  /  管理员 发布于 3年前   281
数字输入框组件 实现功能:只允许输入数字(包括小数)、允许设置初始值、最大值、最小值。 为了方便,这里选用Vue的 cli-service 实现快速原型开发 首先template部分代码 这部分没有什么特别说明的,分别传入 value、max、min 作为子组件的原始值最大值和最小值。在子组件中用 props 接收,独立组件,对每个传入的prop进行类型验证 主要JS部分代码 在这里不能使用字符串的方式定义组件模板,所以使用 render() 函数的方式 定义 watch 和 methods 最后是Less部分代码 总结 以上所述是小编给大家介绍的Vue数字输入框组件示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
render(cr) { let _this = this return cr('div',{'class': 'input-number'},[ cr('button',{'class': {'down-btn':true,'dis':this.currentValue<=this.min},on: {click: _this.handleDown},},['-']), cr('input',{'class': 'change-input',domProps: {value: _this.currentValue}, on: {change: _this.handleChange}}), cr('button',{'class': {'down-btn':true,'dis':this.currentValue>=this.max},on: {click: _this.handleUp},},['+']), ])}
watch: { value(val) { this.updateValue(val) }, currentValue(val) { this.$emit('input', val) this.$emit('on-change', val) }},methods: { updateValue(val) { if(val > this.max) val = this.max if(val < this.min) val = this.min this.currentValue = val }, handleDown() { if(this.currentValue<=this.min) return this.currentValue-=1 }, handleUp() { if(this.currentValue>=this.max) return this.currentValue+=1 }, handleChange(ev) { let val = ev.target.value.trim() let max = this.max let min = this.min if(this.isValueNumber(val)) { val = Number(val) this.currentValue = val if(val > max) { this.currentValue = max } else if(val < min) { this.currentValue = min } }else { ev.target.value = this.currentValue } }, isValueNumber(val) { return (/(^-?[0-9]+\.{1}\d+$)|(^-?[1-9][0-9]*$)|(^-?0{1}$)/).test(val+'') }}
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!您可能感兴趣的文章:
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号