Skip to content

Commit f0dca9d

Browse files
committed
修改数字输入框双向绑定问题
1 parent b65d277 commit f0dca9d

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/components/inputNumber.vue

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<v-text-field @blur="onBlur" dense flat solo background-color="info" @keypress="onKeypress" v-model="value"
2+
<v-text-field @blur="onBlur" dense flat solo background-color="info" @keypress="onKeypress" v-model="inputVal"
33
class="v-input-number" :hide-details="hideDetails">
44
<template v-slot:prepend-inner>
55
<v-btn icon class="icon-btn" @click="subtract">
@@ -32,16 +32,29 @@ export default {
3232
},
3333
hideDetails: {
3434
type: Boolean,
35-
default: false
36-
}
35+
default: false,
36+
},
3737
},
3838
data() {
39-
return {}
39+
return {
40+
inputVal: '',
41+
}
42+
},
43+
mounted() {
44+
this.inputVal = this.value
45+
},
46+
watch: {
47+
inputVal(newVal) {
48+
this.emitEvent(newVal)
49+
},
4050
},
4151
methods: {
52+
emitEvent(val) {
53+
this.$emit('input', val)
54+
},
4255
onBlur() {
43-
let num = parseInt(this.value, 10)
44-
this.value = Number.isNaN(num) || num < this.min ? this.min : num
56+
let num = parseInt(this.inputVal, 10)
57+
this.inputVal = Number.isNaN(num) || num < this.min ? this.min : num
4558
},
4659
onKeypress(e) {
4760
const keyCode = e.keyCode
@@ -53,14 +66,14 @@ export default {
5366
}
5467
5568
// 如果输入的第一位是0,输入的下一个字符会自动覆盖0
56-
if (this.value == '0') this.value = ''
69+
if (this.inputVal == '0') this.inputVal = ''
5770
},
5871
subtract() {
59-
if (this.value < this.min + this.step) return void 0
60-
this.value -= this.step
72+
if (this.inputVal < this.min + this.step) return void 0
73+
this.inputVal -= this.step
6174
},
6275
add() {
63-
this.value += this.step
76+
this.inputVal += this.step
6477
},
6578
},
6679
components: {},

0 commit comments

Comments
 (0)