Vue數(shù)字輸入框組件使用方法詳解
前面的話
關(guān)于基礎(chǔ)組件介紹,已經(jīng)更新完了。這篇文章將用組件基礎(chǔ)知識(shí)開(kāi)發(fā)一個(gè)數(shù)字輸入框組件。將涉及到指令、事件、組件間通信。
基礎(chǔ)需求
- 只能輸入數(shù)字
- 設(shè)置初始值,最大值,最小值
- 在輸入框聚焦時(shí),增加對(duì)鍵盤(pán)上下鍵的支持
- 增加一個(gè)控制步伐prop-step,例如,設(shè)置為10 ,點(diǎn)擊加號(hào)按鈕,一次增加10
項(xiàng)目搭建
在了解需求后,進(jìn)行項(xiàng)目的初始化:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> </head> <body> <div id="app"> <input-number></input-number> </div> </body> </html> <script> Vue.component('input-number',{ template: ` <div class="input-number"> <input type="text" /> <button>-</button> <button>+</button> </div> `} var app = new Vue({ el:'#app' }) </script>
初始化結(jié)構(gòu)搭好后,由于要設(shè)置初始值,最大值、最小值,我們?cè)诟附M件中的 < input-number>< /input-number>上設(shè)置這些值,并且通過(guò)Props將父組件數(shù)據(jù)傳遞給子組件
父組件中添加數(shù)據(jù):value是一個(gè)關(guān)鍵的綁定值,所以用v-model,實(shí)現(xiàn)雙向綁定。
<input-number v-model="value" :max="100" :min="0"></input-number>
在子組件中添加props選項(xiàng):
props: { max: { type: Number, default: Infinity }, min: { type: Number, default: -Infinity }, value: { type: Number, default: 0 } },
我們知道,Vue組件時(shí)單項(xiàng)數(shù)據(jù)流,所以無(wú)法在子組件上更改父組件傳遞的數(shù)據(jù),在這里子組件只改變value值,所以我們給子組件設(shè)置一個(gè)data數(shù)據(jù),默認(rèn)引用value值,然后在組件內(nèi)部維護(hù)這個(gè)data。
data() { return{ // 保存初次父組件傳遞的數(shù)據(jù) currentValue : this.value, } }
并且給子組件的input元素綁定value
<input type="text" :value="currentValue" />
這樣只解決了初始化時(shí)引用父組件value的問(wèn)題,但是如果父組件修改了value,子組件無(wú)法感知,我們想要currentValue一起更新。那么就要使用wacth監(jiān)聽(tīng)選項(xiàng)監(jiān)聽(tīng)value。
- 當(dāng)父組件value發(fā)生變化時(shí),子組件currentValue也跟著變化。
- 當(dāng)子組件currentValue變化時(shí),使用$emit觸發(fā)v-model,使得父組件value也跟著變化
watch: { // 監(jiān)聽(tīng)屬性currentValue與value currentValue(val) { // currentValue變化時(shí),通知父組件的value也變化 this.$emit('input', val); }, value(val) { // 父組件value改變時(shí),子組件currentValue也改變 this.updataValue(val); } }, methods: { updataValue(val) { if(val > this.max) val = this.max; if(val < this.min) val = this.min; this.currentValue = val; } }, // 第一次初始化時(shí),也對(duì)value進(jìn)行過(guò)濾 mounted: function() { this.updataValue(this.value); }
上述代碼中,生命周期mounted鉤子也使用了updateValue()方法,是因?yàn)槌跏蓟瘯r(shí)也要對(duì)value進(jìn)行驗(yàn)證。
父子間的通信解決差不多了,接下來(lái)完成按鈕的操作:添加handleDown與handleUp方法
template: ` <div class="input-number"> <input type="text" :value="currentValue" /> <button @click="handleDown" :disabled="currentValue <= min">-</button> <button @click="handleUp" :disabled="currentValue >= max">+</button> </div> `, methods: { updataValue(val) { if(val > this.max) val = this.max; if(val < this.min) val = this.min; this.currentValue = val; }, // 點(diǎn)擊減號(hào) 減10 handleDown() { if(this.currentValue < this.min) return this.currentValue -= this.prop_step; }, // 點(diǎn)擊加號(hào) 加10 handleUp() { if(this.currentValue < this.min) return this.currentValue += this.prop_step; },
當(dāng)用戶在輸入框想輸入具體的值時(shí),怎么辦?
為input綁定原生change事件,并且判斷輸入的是否數(shù)字:
<input type="text" :value="currentValue" @change="handleChange" />
在methods選項(xiàng)中,添加handleChange方法:
// 輸入框輸入值 handleChange(event) { var val = event.target.value.trim(); var max = this.max; var min = this.min; if(isValueNumber(val)) { val = Number(val); if(val > max) { this.currentValue = max; } if(val < min) { this.currentValue = min; } this.currentValue = val; console.log(this.value); }else { event.target.value = this.currentValue; }
在外層添加判斷是否為數(shù)字的方法isValueNumber:
function isValueNumber(value) { return (/(^-?[0-9]+\.{1}\d+$)|(^-?[1-9][0-9]*$)|(^-?0{1}$)/).test(value+ ''); }
到此一個(gè)數(shù)字輸入框組件基本完成,但是前面提出的后兩個(gè)要求也需要實(shí)現(xiàn),很簡(jiǎn)單,在input上綁定一個(gè)keydown事件,在data選項(xiàng)中添加數(shù)據(jù)prop_step
<input type="text" :value="currentValue" @change="handleChange" @keydown="handleChange2" />
data() { return{ // 保存初次父組件傳遞的數(shù)據(jù) currentValue : this.value, prop_step: 10 } }
// 當(dāng)聚焦時(shí),按上下鍵改變 handleChange2(event) { console.log(event.keyCode) if(event.keyCode == '38') { this.currentValue ++; } if(event.keyCode == '40') { this.currentValue --; } }
完整代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="https://unpkg.com/vue/dist/vue.js"></script> </head> <body> <div id="app"> <input-number v-model="value" :max="100" :min="0"></input-number> </div> </body> </html> <script> function isValueNumber(value) { return (/(^-?[0-9]+\.{1}\d+$)|(^-?[1-9][0-9]*$)|(^-?0{1}$)/).test(value+ ''); } Vue.component('input-number',{ props: { max: { type: Number, default: Infinity }, min: { type: Number, default: -Infinity }, value: { type: Number, default: 0 } }, template: ` <div class="input-number"> <input type="text" :value="currentValue" @change="handleChange" @keydown="handleChange2" /> <button @click="handleDown" :disabled="currentValue <= min">-</button> <button @click="handleUp" :disabled="currentValue >= max">+</button> </div> `, data() { return{ // 保存初次父組件傳遞的數(shù)據(jù) currentValue : this.value, prop_step: 10 } }, watch: { // 監(jiān)聽(tīng)屬性currentValue與value currentValue(val) { // currentValue變化時(shí),通知父組件的value也變化 this.$emit('input', val); }, value(val) { // 父組件value改變時(shí),子組件currentValue也改變 this.updataValue(val); } }, methods: { updataValue(val) { if(val > this.max) val = this.max; if(val < this.min) val = this.min; this.currentValue = val; }, // 點(diǎn)擊減號(hào) 減10 handleDown() { if(this.currentValue < this.min) return this.currentValue -= this.prop_step; }, // 點(diǎn)擊加號(hào) 加10 handleUp() { if(this.currentValue < this.min) return this.currentValue += this.prop_step; }, // 輸入框輸入值 handleChange(event) { var val = event.target.value.trim(); var max = this.max; var min = this.min; if(isValueNumber(val)) { val = Number(val); if(val > max) { this.currentValue = max; } if(val < min) { this.currentValue = min; } this.currentValue = val; console.log(this.value); }else { event.target.value = this.currentValue; } }, // 當(dāng)聚焦時(shí),按上下鍵改變 handleChange2(event) { console.log(event.keyCode) if(event.keyCode == '38') { this.currentValue ++; } if(event.keyCode == '40') { this.currentValue --; } } }, // 第一次初始化時(shí),也對(duì)value進(jìn)行過(guò)濾 mounted: function() { this.updataValue(this.value); } }) var app = new Vue({ el:'#app', data:{ value: 5 } }) </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中使用AJAX實(shí)現(xiàn)讀取來(lái)自XML文件的信息
這篇文章主要為大家詳細(xì)介紹了vue中如何使用AJAX實(shí)現(xiàn)讀取來(lái)自XML文件的信息,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的小伙伴可以參考下2023-12-12關(guān)于Vue?CLI3中啟動(dòng)cli服務(wù)參數(shù)說(shuō)明
這篇文章主要介紹了關(guān)于Vue?CLI3中啟動(dòng)cli服務(wù)參數(shù)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue中input獲取光標(biāo)位置并追加內(nèi)容
這篇文章主要給大家介紹了關(guān)于vue中input獲取光標(biāo)位置并追加內(nèi)容的相關(guān)資料,vue通過(guò)當(dāng)前的光標(biāo)來(lái)進(jìn)行插值,從而需要去獲取光標(biāo)所在的位置,需要的朋友可以參考下2023-07-07vue實(shí)現(xiàn)下拉加載其實(shí)沒(méi)那么復(fù)雜
這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)下拉加載的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue3通過(guò)render函數(shù)實(shí)現(xiàn)菜單下拉框的示例
本文主要介紹了vue3通過(guò)render函數(shù)實(shí)現(xiàn)菜單下拉框的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04一步步教你搭建VUE+VScode+elementUI開(kāi)發(fā)環(huán)境
這篇文章主要給大家介紹了關(guān)于搭建VUE+VScode+elementUI開(kāi)發(fā)環(huán)境的相關(guān)資料,近期被配置環(huán)境的事情弄得整個(gè)人都要炸了,現(xiàn)在整理如下,希望有相同需求的朋友可以不用走彎路,需要的朋友可以參考下2023-07-07vue3+高德地圖只展示指定市、區(qū)行政區(qū)域的地圖以及遮罩反向鏤空其他地區(qū)
vue大屏項(xiàng)目開(kāi)發(fā),客戶覺(jué)得地圖上的文字標(biāo)注太多了,要求地圖上只顯示省市等主要城市的標(biāo)注,這篇文章主要給大家介紹了關(guān)于vue3+高德地圖只展示指定市、區(qū)行政區(qū)域的地圖以及遮罩反向鏤空其他地區(qū)的相關(guān)資料,需要的朋友可以參考下2024-02-02vue3圖片剪裁插件vue-img-cutter使用小結(jié)
Vue.js是一款流行的JavaScript前端框架,很受用戶喜愛(ài),這篇文章主要介紹了vue3圖片剪裁插件vue-img-cutter使用小結(jié),本文結(jié)合示例代碼講解的非常詳細(xì),感興趣的朋友一起看看吧2024-01-01