解决问题:当用户输入其他字符的时候自动回退到 oldValue
<template>
<Input placeholder="用户Id" search enter-button v-model="userId" />
</template>
<script>
export default {
data() {
return {
userId: ''
}
},
watch: {
userId(newValue, oldValue) {
if (!Number(newValue)) {
this.$nextTick(() => {
this.userId = oldValue
})
}
}
}
}
</script>
读了源码,不写那个 $nextTick 是没法改成功的,具体原因,大家可以阅读他的源码获取,这里只给大家解决方法