How vue filters are assigned

Vue.directive("numbers",{
  bind (el, binding) {
    el.oninput = function (data) {
      this.value = this.value.replace(/\D/g, "")
    }
  }
})

I want to use the input event, but I can"t assign a value to the input box
the requirement is that the input box can only enter numbers


<div v-numbers class="el-input">
      <input  type="text" v-model="a" >
</div>

this is how I add events to the child elements of the instruction, because I want to add an instruction to the el-input, but I can"t bind it

.
Mar.28,2021

doesn't know what you want, but if it's just to limit the number you can enter, just use < input type= "number" > .


vmurmodel.number = "a" can I use


my approach is to encapsulate this as a custom Input component
to receive the value,type value. After the input triggers the change event, it validates and converts the format of the string, and then re-assigns the value, triggering the v-model update

. < hr >

ide/04-vue-components/element-ui-input.html" rel=" nofollow noreferrer "> can refer to , and you can transform it according to your project

.

component code

<template>

  <el-input :type="type" v-model="currentValue" :maxlength="maxlength" :disabled="disabled" :class="small?'inline-small-input':''" :placeholder="placeholder">
    <slot name="prefix" slot="prefix"></slot>
    <slot name="suffix" slot="suffix"></slot>
    <slot name="prepend" slot="prepend"></slot>
    <slot name="append" slot="append"></slot>
  </el-input>
</template>

<script>
export default {
  props: {
    value: {
      type: [String, Number]
    },
    type: {
      type: String,
      default: 'text'
    },
    placeholder: {
      type: String
    },
    maxlength: {
      type: Number
    },
    small: {
      type: Boolean,
      default: false
    },
    validate: {
      type: String,
      default: '' // number float
    },
    fixed: {
      type: Number,
      default: -1
    },
    disabled: {
      type: Boolean,
      default: false
    },
    min: {
      type: Number
    },
    max: {
      type: Number
    }
  },
  data() {
    return {
      currentValue: null
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val !== this.currentValue) {
          this.currentValue = val
        }
      },
      immediate: true
    },
    currentValue(val, oldVal) {
      this.$nextTick(() => {
        this.currentValue = this.convert(val)
        this.$emit('input', this.currentValue)
      })
    }
  },
  methods: {
    convert(val) {
      switch (this.validate) {
        case 'number':
          val = parseInt(this.currentValue) || 0
          break
        case 'z-number':
          val = Math.max(parseInt(this.currentValue) || 0, 0)
          break
        case 'f-number':
          val = Math.min(parseInt(this.currentValue) || 0, 0)
          break
        case 'float':
          val = parseFloat(this.currentValue) || 0
          val = this.fixed > -1 ? val.toFixed(this.fixed) : val
          break
        case 'z-float':
          val = Math.max(parseFloat(this.currentValue) || 0, 0)
          val = this.fixed > -1 ? val.toFixed(this.fixed) : val
          break
        case 'f-float':
          val = Math.min(parseFloat(this.currentValue) || 0, 0)
          val = this.fixed > -1 ? val.toFixed(this.fixed) : val
          break
        default:
          break
      }
      if (this.validate !== '') {
        if (typeof this.max === 'number') {
          val = Math.min(this.max, val)
        }
        if (typeof this.min === 'number') {
          val = Math.max(val, this.min)
        }
      }
      return val
    }
  }
}
</script>

use

<v-form-input v-model="inputValue" validate="z-number" :min="1" :max="1"/>
Menu