I
I
Igor2021-08-31 11:10:43
typescript
Igor, 2021-08-31 11:10:43

How to specify data type for input using Vuejs and TS?

For example, I have such a code and TS swears that input is a string when I add a number to the entered data. But if I put 0 in the date, then it is put in iput and the placeholder is not visible. How can I correctly specify the type here or get around that so that 0 is not written instead of placeholder.

<template>
      <div>
        <input type="number" v-model.number='test' placeholder="placeholder">
      </div>
</template>

<script lang='ts'>
import { defineComponent } from 'vue';
export default defineComponent({
  name: "inp",
  data() {
    return {
      test: '',
    }
  },
  methods:{
    plus(){
      this.test += 10 
    }
  }
})
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-08-31
@bingo347

Correctly swears, because if you do it to an empty string += 10, the result will be '10', and then '1010'- this is clearly not what you expect.

plus() {
  this.test = `${+this.test + 10}`
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question