Vue數(shù)字輸入框組件使用方法詳解
前面的話(huà)
關(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)用戶(hù)在輸入框想輸入具體的值時(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)文章
vue2.x中h函數(shù)(createElement)與vue3中的h函數(shù)詳解
h函數(shù)本質(zhì)就是createElement(),h函數(shù)其實(shí)是createVNode的語(yǔ)法糖,返回的就是一個(gè)Js普通對(duì)象,下面這篇文章主要給大家介紹了關(guān)于vue2.x中h函數(shù)(createElement)與vue3中h函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-12-12
vue使用better-scroll實(shí)現(xiàn)滑動(dòng)以及左右聯(lián)動(dòng)
這篇文章主要介紹了vue使用better-scroll實(shí)現(xiàn)滑動(dòng)以及左右聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Vue+iview+webpack ie瀏覽器兼容簡(jiǎn)單處理
這篇文章主要介紹了Vue+iview+webpack ie瀏覽器兼容簡(jiǎn)單處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
解決vue下載后臺(tái)傳過(guò)來(lái)的亂碼流的問(wèn)題
這篇文章主要介紹了解決vue下載后臺(tái)傳過(guò)來(lái)的亂碼流的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Vue過(guò)濾器,生命周期函數(shù)和vue-resource簡(jiǎn)單介紹
這篇文章主要介紹了Vue過(guò)濾器,生命周期函數(shù)和vue-resource簡(jiǎn)單介紹,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-01-01
vue.config.js中配置分包策略及常見(jiàn)的配置選項(xiàng)
在Vue.js中分包(Code Splitting)是一種將應(yīng)用程序代碼拆分為不同的塊或包的技術(shù),從而在需要時(shí)按需加載這些包,下面這篇文章主要給大家介紹了關(guān)于vue.config.js中配置分包策略及常見(jiàn)的配置選項(xiàng)的相關(guān)資料,需要的朋友可以參考下2024-02-02
Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼
本篇文章主要介紹了Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-07-07
單頁(yè)面vue引入百度統(tǒng)計(jì)的使用方法示例詳解
在網(wǎng)上各種找不到vue項(xiàng)目加入百度統(tǒng)計(jì)的代碼與實(shí)現(xiàn),自己探索出了一套加入百度統(tǒng)計(jì)的辦法,下面這篇文章主要給大家介紹了關(guān)于單頁(yè)面vue引入百度統(tǒng)計(jì)的使用方法,需要的朋友可以參考下2018-10-10

