我們對(duì)普通輸入框進(jìn)行擴(kuò)展,實(shí)現(xiàn)一個(gè)可快捷輸入數(shù)字組件。
創(chuàng)新互聯(lián)專注于蘭溪企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城開(kāi)發(fā)。蘭溪網(wǎng)站建設(shè)公司,為蘭溪等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
首先制定規(guī)則:
接著,規(guī)劃好 API。一個(gè) Vue.js 組件最重要的 3 個(gè)部分就是 props、events 以及 slot,我們需要定義這三個(gè)部分的命名以及業(yè)務(wù)規(guī)則。這個(gè)組件比較簡(jiǎn)單,所以我們只用到 props 與 events。
1 基礎(chǔ)版
html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>數(shù)字輸入組件</title> </head> <body> <div id="app"> <number-input v-model="value" :min="0" :max="6"></number-input> </div> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> <script src="number.js"></script> <script> var app = new Vue({ el: '#app', data: { value: 3 } }); </script> </body> </html>
這里,我們使用了 v-model,雙向綁定了 value。
number.js:
/** * 是否為數(shù)字 * @param val * @returns {boolean} */ function isNum(val) { return (/^[0-9]*$/).test(val); } /** * 數(shù)字輸入組件 */ Vue.component('number-input', { template: '\ <div class="number-input">\ <input \ type="text"\ :value="currentVal"\ @change="change">\ <button\ @click="down"\ :disabled="currentVal<=min">-</button>\ <button\ @click="up"\ :disabled="currentVal >=max">+</button>\ </div>', props: {//校驗(yàn) //最大值 max: { type: Number, default: Infinity }, //最小值 min: { type: Number, default: -Infinity }, //初始值 value: { type: Number, default: 0 } }, data: function () { return { currentVal: this.value } }, watch: { currentVal: function (val) { console.log("currentVal:" + this.currentVal); this.$emit('input',val); }, value: function (val) {//更新 currentVal this.update(val); } }, methods: { /** * 更新 * @param val */ update: function (val) { //讓輸入的值在限定范圍內(nèi) if (val > this.max) { val = this.max; } if (val < this.min) { val = this.min } this.currentVal = val; }, /** * 減少 */ down: function () { if (this.currentVal <= this.min) { return; } this.currentVal -= 1; }, /** * 增長(zhǎng) */ up: function () { if (this.currentVal >= this.max) { return; } this.currentVal += 1; }, /** * 如果輸入的值, * @param event */ change: function (event) { var val = event.target.value.trim();//獲取輸入值 if (isNum(val)) {//賦值 currentVal val = Number(val); this.currentVal = val; //超出限定范圍時(shí),規(guī)整 var max = this.max; var min = this.min; if (val > max) { this.currentVal = max; } else if (val < min) { this.currentVal = min; } } else {//還原為 currentVal event.target.value = this.currentVal; } } }, mounted: function () { //對(duì)初始值進(jìn)行范圍限定 this.update(this.value); } });
這里,我們專門定義了一個(gè) number.js,用于定義數(shù)字輸入組件。
在 number.js 中,我們做了如下工作:
效果:
2 按鍵支持
當(dāng)輸入框獲得焦點(diǎn)時(shí),按下“向上鍵”時(shí),增長(zhǎng);按下“向上鍵”時(shí),減少。
這可以利用按鍵修飾符來(lái)實(shí)現(xiàn),我們修改示例中的組件模板:
... <input type="text" :value="currentVal" @change="change" @keyup.up="up" @keyup.down="down"> ...
Vue.js 定義按鍵別名有這些:
效果:
3 控制步伐
新增一個(gè)步伐屬性,增長(zhǎng)或者減少以步伐值為變化量。之前的示例,步伐為 1。
首先在 props 中,定義一個(gè)步伐屬性:
//步伐 step: { type: Number, default: 1 }
然后在增長(zhǎng)與減少函數(shù)中,使用步伐屬性做為變化量:
/** * 減少 */ down: function () { if (this.currentVal <= this.min) { return; } this.currentVal -= this.step; }, /** * 增長(zhǎng) */ up: function () { if (this.currentVal >= this.max) { return; } this.currentVal += this.step; },
最后為組件重新配置參數(shù):
<number-input v-model="value" :min="0" :max="50" :step="5"></number-input>
效果:
本文示例代碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站欄目:說(shuō)說(shuō)如何在Vue.js中實(shí)現(xiàn)數(shù)字輸入組件的方法
鏈接分享:http://chinadenli.net/article48/ppgehp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、網(wǎng)站策劃、網(wǎng)站內(nèi)鏈、網(wǎng)站建設(shè)、App開(kāi)發(fā)、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容