父组件:
<input-number v-model="number" :min="min" :max="max"></input-number>
子组件
<template>
<div>
<input :value="currentnumber" @change="handleChange" />
<button v-on:keyup.up="handleUp" @click="handleUp" :disabled="currentnumber >= max">+</button>
<button @click="handleDown" :disabled="currentnumber <= min">-</button>
</div>
</template>
<script>
export default {
data() {
return {
currentnumber: this.value
};
},
props: {
value: {
type: Number,
default: 0
},
max: {
type: Number,
default: Infinity
},
min: {
type: Number,
default: -Infinity
}
},
components: {
},
watch: {
value: function (val, oldvalue) {
this.checknumber(val);
},
currentnumber: function (val, oldvalue) {
this.$emit('input', val);
}
},
computed: {
},
methods: {
handleChange(event) {
var val = event.target.value;
this.currentnumber = val;
if (val > this.max) {
this.currentnumber = this.max;
} else if (val < this.min) {
this.currentnumber = this.min;
}
},
handleUp() {
if (this.currentnumber >= this.max) return;
this.currentnumber += 1;
},
handleDown() {
if (this.currentnumber <= this.min) return;
else this.currentnumber--;
},
checknumber(data) {
if (data > this.max) data = this.max;
if (data < this.min) data = this.min;
this.currentnumber = data;
}
},
mounted: function () {
this.checknumber(this.value);
},
}
</script>
<style lang='scss' scoped>
</style>