Skip to content

Instantly share code, notes, and snippets.

@reslear
Last active January 7, 2021 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reslear/7564a9ed30607a73a637e47abbc32f3b to your computer and use it in GitHub Desktop.
Save reslear/7564a9ed30607a73a637e47abbc32f3b to your computer and use it in GitHub Desktop.
vue forceUpdate v-on input in slots

vue forceUpdate v-on input in slots

typescript

  computed: {
    inputEvents(): any {
      return {
        input: (e: { target: HTMLInputElement }) => {
          const value = e.target.value;
          const result = value.replace(/\D+/g, '');

          if (value !== result) {
            e.target.value = result;
          }

          this.$emit("input", result);
        },
      };
    }
  }

based on source vuejs/vue#10716 (comment)

This is working as expected: by filtering out letters before setting myNumericValue, this variable never changes and never triggers a re render, so the value on the input is preserved.

One way to get around this is to force update with this.$forceUpdate() after changing the value. A better way is to reset the input's value only:

const val = $event.target.value.replace(/[^\d]/g, ""); // strips letters, keeps numbers
      if (val === this.numericValue) $event.target.value = val
      else this.numericValue = val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment