Vue數(shù)字輸入框組件使用方法詳解
前面的話
關(guān)于基礎(chǔ)組件介紹,已經(jīng)更新完了。這篇文章將用組件基礎(chǔ)知識開發(fā)一個數(shù)字輸入框組件。將涉及到指令、事件、組件間通信。

基礎(chǔ)需求
- 只能輸入數(shù)字
- 設(shè)置初始值,最大值,最小值
- 在輸入框聚焦時,增加對鍵盤上下鍵的支持
- 增加一個控制步伐prop-step,例如,設(shè)置為10 ,點擊加號按鈕,一次增加10
項目搭建
在了解需求后,進行項目的初始化:
<!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è)置初始值,最大值、最小值,我們在父組件中的 < input-number>< /input-number>上設(shè)置這些值,并且通過Props將父組件數(shù)據(jù)傳遞給子組件
父組件中添加數(shù)據(jù):value是一個關(guān)鍵的綁定值,所以用v-model,實現(xiàn)雙向綁定。
<input-number v-model="value" :max="100" :min="0"></input-number>
在子組件中添加props選項:
props: {
max: {
type: Number,
default: Infinity
},
min: {
type: Number,
default: -Infinity
},
value: {
type: Number,
default: 0
}
},
我們知道,Vue組件時單項數(shù)據(jù)流,所以無法在子組件上更改父組件傳遞的數(shù)據(jù),在這里子組件只改變value值,所以我們給子組件設(shè)置一個data數(shù)據(jù),默認引用value值,然后在組件內(nèi)部維護這個data。
data() {
return{
// 保存初次父組件傳遞的數(shù)據(jù)
currentValue : this.value,
}
}
并且給子組件的input元素綁定value
<input type="text" :value="currentValue" />
這樣只解決了初始化時引用父組件value的問題,但是如果父組件修改了value,子組件無法感知,我們想要currentValue一起更新。那么就要使用wacth監(jiān)聽選項監(jiān)聽value。
- 當(dāng)父組件value發(fā)生變化時,子組件currentValue也跟著變化。
- 當(dāng)子組件currentValue變化時,使用$emit觸發(fā)v-model,使得父組件value也跟著變化
watch: {
// 監(jiān)聽屬性currentValue與value
currentValue(val) {
// currentValue變化時,通知父組件的value也變化
this.$emit('input', val);
},
value(val) {
// 父組件value改變時,子組件currentValue也改變
this.updataValue(val);
}
},
methods: {
updataValue(val) {
if(val > this.max) val = this.max;
if(val < this.min) val = this.min;
this.currentValue = val;
}
},
// 第一次初始化時,也對value進行過濾
mounted: function() {
this.updataValue(this.value);
}
上述代碼中,生命周期mounted鉤子也使用了updateValue()方法,是因為初始化時也要對value進行驗證。
父子間的通信解決差不多了,接下來完成按鈕的操作:添加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;
},
// 點擊減號 減10
handleDown() {
if(this.currentValue < this.min) return
this.currentValue -= this.prop_step;
},
// 點擊加號 加10
handleUp() {
if(this.currentValue < this.min) return
this.currentValue += this.prop_step;
},
當(dāng)用戶在輸入框想輸入具體的值時,怎么辦?
為input綁定原生change事件,并且判斷輸入的是否數(shù)字:
<input type="text" :value="currentValue" @change="handleChange" />
在methods選項中,添加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+ '');
}
到此一個數(shù)字輸入框組件基本完成,但是前面提出的后兩個要求也需要實現(xiàn),很簡單,在input上綁定一個keydown事件,在data選項中添加數(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)聚焦時,按上下鍵改變
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)聽屬性currentValue與value
currentValue(val) {
// currentValue變化時,通知父組件的value也變化
this.$emit('input', val);
},
value(val) {
// 父組件value改變時,子組件currentValue也改變
this.updataValue(val);
}
},
methods: {
updataValue(val) {
if(val > this.max) val = this.max;
if(val < this.min) val = this.min;
this.currentValue = val;
},
// 點擊減號 減10
handleDown() {
if(this.currentValue < this.min) return
this.currentValue -= this.prop_step;
},
// 點擊加號 加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)聚焦時,按上下鍵改變
handleChange2(event) {
console.log(event.keyCode)
if(event.keyCode == '38') {
this.currentValue ++;
}
if(event.keyCode == '40') {
this.currentValue --;
}
}
},
// 第一次初始化時,也對value進行過濾
mounted: function() {
this.updataValue(this.value);
}
})
var app = new Vue({
el:'#app',
data:{
value: 5
}
})
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue2.x中h函數(shù)(createElement)與vue3中的h函數(shù)詳解
h函數(shù)本質(zhì)就是createElement(),h函數(shù)其實是createVNode的語法糖,返回的就是一個Js普通對象,下面這篇文章主要給大家介紹了關(guān)于vue2.x中h函數(shù)(createElement)與vue3中h函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-12-12
vue使用better-scroll實現(xiàn)滑動以及左右聯(lián)動
這篇文章主要介紹了vue使用better-scroll實現(xiàn)滑動以及左右聯(lián)動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
Vue過濾器,生命周期函數(shù)和vue-resource簡單介紹
這篇文章主要介紹了Vue過濾器,生命周期函數(shù)和vue-resource簡單介紹,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-01-01

