How does ivew-design's Input component restrict him to input numbers only (if it is wrong, it will fall back to oldValue)

 Solve the problem: automatically fall back to oldValue when the user enters other characters

<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>

After reading the source code, if you don’t write $nextTick, you can’t change it successfully. For the specific reason, you can read his source code to get it. Here we only give you the solution

Guess you like

Origin blog.csdn.net/weixin_42335036/article/details/121213025