ivew-design 的 Input 组件如何限制只让他输入数字(错误的话回退到 oldValue)

 解决问题:当用户输入其他字符的时候自动回退到 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 是没法改成功的,具体原因,大家可以阅读他的源码获取,这里只给大家解决方法

猜你喜欢

转载自blog.csdn.net/weixin_42335036/article/details/121213025