Vue數(shù)字輸入框組件示例代碼詳解
數(shù)字輸入框組件
實(shí)現(xiàn)功能:只允許輸入數(shù)字(包括小數(shù))、允許設(shè)置初始值、最大值、最小值。
為了方便,這里選用Vue的 cli-service
實(shí)現(xiàn)快速原型開發(fā)
首先template部分代碼
<template> <div class="demo"> <input-number v-model="value" :max="10" :min="0"></input-number> </div> </template>
這部分沒有什么特別說明的,分別傳入 value、max、min 作為子組件的原始值最大值和最小值。在子組件中用 props 接收,獨(dú)立組件,對每個(gè)傳入的prop進(jìn)行類型驗(yàn)證
主要JS部分代碼
<script> import Vue from 'vue' Vue.component('input-number',{ props: { value: { type: Number, default: 0 }, max: { type: Number, default: Infinity }, min: { type: Number, default: -Infinity } }, data() { return { currentValue: this.value } }, render(cr) { let _this = this ... } }) export default { data() { return { value: 5 } } } </script>
在這里不能使用字符串的方式定義組件模板,所以使用 render() 函數(shù)的方式
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 和 methods
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+'') } }
最后是Less部分代碼
<style lang="less"> * { box-sizing: border-box; } .demo { width: 200px; margin: 0 auto; } .input-number { width: 100%; display: flex; height: 40px; align-items: center; justify-content: space-between; .down-btn,.up-btn { font-size: 18px; width: 40px; height: 40px; background-color: #f5f7fa; color: #606266; border: 1px solid #dcdfe6; border-radius: 4px 0 0 4px; cursor: pointer; outline: none; &.up-btn { border-radius: 0 4px 4px 0; } &.dis { cursor: not-allowed; } } .change-input { flex: 1; max-width: 100px; outline: none; border: none; text-align: center; height: 40px; } } </style>
總結(jié)
以上所述是小編給大家介紹的Vue數(shù)字輸入框組件示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
vue3中安裝使用vue-i18n實(shí)時(shí)切換語言且不用刷新
這篇文章主要介紹了vue3中安裝使用vue-i18n實(shí)時(shí)切換語言不用刷新問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Vue過濾器filters的用法及時(shí)間戳轉(zhuǎn)換問題
Vue的filters過濾器是比較常見的一個(gè)知識點(diǎn),下面我將結(jié)合時(shí)間戳轉(zhuǎn)換的例子帶你快速了解filters的用法,感興趣的朋友一起看看吧2021-09-09Vue2?Element?description組件列合并詳解
在使用Vue的時(shí)候經(jīng)常會涉及到表格的列合并,下面這篇文章主要給大家介紹了給大家Vue2?Element?description組件列合并的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01vue-cli3項(xiàng)目在IE瀏覽器打開兼容問題及解決
這篇文章主要介紹了vue-cli3項(xiàng)目在IE瀏覽器打開兼容問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue react中的excel導(dǎo)入和導(dǎo)出功能
當(dāng)我們把信息化系統(tǒng)給用戶使用時(shí),用戶經(jīng)常需要把以前在excel里錄入的數(shù)據(jù)導(dǎo)入的信息化系統(tǒng)里,這樣為用戶提供了很大的方便,這篇文章主要介紹了vue中或者react中的excel導(dǎo)入和導(dǎo)出,需要的朋友可以參考下2023-09-09Vue3源碼分析reactivity實(shí)現(xiàn)方法示例
這篇文章主要為大家介紹了Vue3源碼分析reactivity實(shí)現(xiàn)方法原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01