1

So I have an input like so:

<input type="number" :value="key" @input.number="setValue" />

I would like for the .number input modifier to be dynamic and linked to a variable, such that I can change it based on the input type (which in my instance can be either number or text).

0

2 Answers 2

2

If you want to use input modifier like that you can always create a custom component which receives type and value as props and emits value on @input -

<template>
  <div>
    <template v-if="type=='number'">
      <input :type="type" :value="key" @input.number="setValue" />
    </template>
    <template v-else>
      <input :type="type" :value="key" @input="setValue">
    </template>
  </div>
</template>
Sign up to request clarification or add additional context in comments.

Comments

2

Just have a handler that does that logic for you in the function.

@input="setValue($event.target.value)"
methods: {
     setValue (input) {
          if (typeof input === 'number') {
                              //do something
                            }
          if (typeof input === 'string') {
                              //do something
                            }
     }
}

You can use any type of check you want to, not necessarily typeof.

3 Comments

Yeah... this is what I ended up going with. I was just wondering if there was a more efficient way of doing it. Thanks!
Just a heads up, I highly recommend for better practice of using a v-model on that input instead of passing the $event.target.value if you aren't already.
Yes, totally agree :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.