0

I have a component like this:

<script>
export default {
  name: "Thing",
  props: {
    min: Number,
    value: Number
  } ...

-

<template>
...
<input type="number" v-bind:min=min/10  step="0.1" v-model = value aria-hidden="true" />
...
</template>

I generally use millimeters, while I want to display things in centimeter. So for example this component will be initialized with value = 50 (50mm), but I want the input to display 5 (5cm).

How can I change "value" in the component to be value/10 upon initialization? Also I need to pass some tests , so something like "Just do value/10 in the parent before initializing the component" wont work Im afraid.

2 Answers 2

1

You can add a computed to the component as follows:

computed: {
    minimum() {
      return this.min / 10;
    }
}

And use it as follows:

<h1>{{ minimum }}</h1>

This way, you pass the prop like this:

:min="min"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. But will this work with "value" ? As you can see I use ``` v-model = value ```. As I said, the input element is supposed to use cm. So if I make it computed and have a v-model, won't that mean that if I change the value in the input field (which is already in cm) it will again be divided by 10 ?
Why don't you use the same concept? And why are you setting the model to a prop? Add more explanation to your scenario and try to link to a demo explaining the problem
0

Found the answer myself:

<script>
export default {
  name: "Thing",
  props: {
    min: Number,
    value: Number
  }, 
   created: function() {
        this.value = this.value/10;
  },...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.