最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue自定義數(shù)字輸入框組件

 更新時(shí)間:2022年06月09日 15:53:53   作者:小雷雷哥哥  
這篇文章主要為大家詳細(xì)介紹了vue自定義數(shù)字輸入框組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近自己在練習(xí)組件開發(fā),做了一個(gè)簡(jiǎn)單的數(shù)字輸入框加減的組件,效果圖如下:

組件可以傳入三個(gè)參數(shù),value是初始化值,max是可輸入的最大值,min是可輸入最小值,當(dāng)然參數(shù)可以按需求繼續(xù)擴(kuò)展的。

組件代碼如下:

<template>
? ? <div style="text-align: center;margin-top: 20px;">
? ? ? ? <input type="text" v-model="currentValue" @change="handleChange">
? ? ? ? <button @click="handleUp" :disabled="currentValue >= max">+</button>
? ? ? ? <button @click="handleDown" :disabled="currentValue <= min">-</button>
? ? </div>
</template>
?
<script>
? ? export default {
? ? ? ? props:['max','min','value'],
? ? ? ? name: "MyInput",
? ? ? ? data(){
? ? ? ? ? ? return {
? ? ? ? ? ? ? ? currentValue:this.value
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? watch:{
? ? ? ? ? ? currentValue: function (val) { //currentValue值變動(dòng)就向父組件傳值
? ? ? ? ? ? ? ? this.$emit('input',val);
? ? ? ? ? ? ? ? this.$emit('on-change',val);
? ? ? ? ? ? },
? ? ? ? ? ? value:function (val) { //對(duì)值進(jìn)行驗(yàn)證
? ? ? ? ? ? ? ? this.updataValue(val);
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? mounted(){
? ? ? ? ? ? this.updataValue(this.value);
? ? ? ? },
? ? ? ? methods:{
? ? ? ? ? ? handleDown: function () { ?//加法
? ? ? ? ? ? ? ? if(this.currentValue <= this.min){
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? this.currentValue -= 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? },
? ? ? ? ? ? handleUp: function () { //減法
? ? ? ? ? ? ? ? if(this.currentValue >= this.max){
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? this.currentValue += 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? },
? ? ? ? ? ? updataValue: function (val) {
? ? ? ? ? ? ? ? if(val > this.max){val = this.max}
? ? ? ? ? ? ? ? if(val < this.min){val = this.min}
? ? ? ? ? ? ? ? this.currentValue = val;
?
? ? ? ? ? ? },
? ? ? ? ? ? handleChange: function (event) { ?//對(duì)值進(jìn)行驗(yàn)證
? ? ? ? ? ? ? ? var val = event.target.value.trim();
? ? ? ? ? ? ? ? var max = this.max;
? ? ? ? ? ? ? ? var min = this.min;
? ? ? ? ? ? ? ? if(this.isNumber(val)){
? ? ? ? ? ? ? ? ? ? val = Number(val);
? ? ? ? ? ? ? ? ? ? this.currentValue = val;
? ? ? ? ? ? ? ? ? ? if(val > max){
? ? ? ? ? ? ? ? ? ? ? ? this.currentValue = max;
? ? ? ? ? ? ? ? ? ? }else if(val < min){
? ? ? ? ? ? ? ? ? ? ? ? this.currentValue = min;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? this.currentValue = 0;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? },
? ? ? ? ? ? isNumber: function (value) {
? ? ? ? ? ? ? ? return (/^\-?[0-9]+$/).test(value + '');
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>
?
<style scoped>
? ? input{
? ? ? ? width: 280px;
? ? ? ? height: 36px;
? ? ? ? padding: 0 10px;
? ? ? ? border: 1px solid #ccc;
? ? ? ? border-radius: 4px;
? ? }
? ? button{
? ? ? ? border: none;
? ? ? ? background: #4e83e4;
? ? ? ? color: #fff;
? ? ? ? height: 36px;
? ? ? ? width: 36px;
? ? }
</style>

調(diào)用組件就很簡(jiǎn)單了,如下:

<template>
? ? <div>
? ? ? ? <h2>數(shù)字輸入框組件</h2>
? ? ? ? <!-- ?max是可輸入的最大值 ?min是可輸入的最小值 ?value是初始值-->
? ? ? ? <my-input v-model="value" :max="10" :min="-5"></my-input>
? ? ? ? <p style="text-align: center;"><button @click="doAlert(value)">輸入框的值是</button></p>
? ? </div>
</template>
?
<script>
? ? import MyInput from '../components/MyInput.vue';
? ? export default {
? ? ? ? name: "computeNumber",
? ? ? ? components:{MyInput},
? ? ? ? data(){
? ? ? ? ? ? return {
? ? ? ? ? ? ? ? value: 1
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? methods:{
? ? ? ? ? ? doAlert: function (val) {
? ? ? ? ? ? ? ? alert(val);
? ? ? ? ? ? }
? ? ? ? }
? ? }
</script>

組件做的很簡(jiǎn)單,歡迎大家一起交流。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

江达县| 平江县| 克拉玛依市| 杭锦后旗| 金山区| 突泉县| 宁强县| 简阳市| 鹤山市| 奈曼旗| 台江县| 荆门市| 安康市| 盐山县| 上饶市| 曲麻莱县| 青岛市| 连南| 长春市| 明光市| 平原县| 太仓市| 长泰县| 绥阳县| 宣化县| 和田市| 临沭县| 千阳县| 永嘉县| 义乌市| 鹰潭市| 舒兰市| 泰来县| 溧阳市| 吉隆县| 门头沟区| 彭泽县| 天全县| 托克逊县| 寿阳县| 宾川县|