I am working on a converter, where I type a number in one box, and it converts and outputs its conversion into another box. For this example, I am using temperatures. The code does what I would expect it to, however, it only does it after the input box loses focus.
For example, if I have a 20 in the first box, I would have a conversion of 20 in the second box. If I delete the 0 in the first box, I would expect it to instantly update in the second box. However as it stands, only after I click into the second box does it update.
<template>
<div id="app">
<form id='calculate'>
<label>Centigrade
<input id='c' v-model.number='cent' type='number' @change='validate()'>
</label>
<label>Fahrenheit
<input id='f' v-model.number='fahren' type='number' @change='validate()'>
</label>
</form>
<!-- {{calculateCentigrade}}
{{calcuateFahrenheit}} -->
</div>
</template>
<script>
export default {
data() {
return {
//(F − 32) × 5/9
//(C × 9/5) + 32
cent: 20,
fahren: 68,
};
},
computed: {
calculateCentigrade(){
return ((this.fahren-32)*(5/9)).toFixed(2)
},
calcuateFahrenheit(){
return ((this.cent*(9/5))+32).toFixed(2)
}
},
methods: {
validate(){
if(event.target.id === 'f'){
this.cent = ((this.fahren-32)*5/9).toFixed(2)
}
else{
this.fahren = ((this.cent*9/5)+32).toFixed(2)
}
}
}
};
</script>
working example: https://codepen.io/PaulRC/pen/WNoqbXY
I thought that it might be that I needed to use computed and if you uncomment this section <!-- {{calculateCentigrade}} {{calcuateFahrenheit}} --> you would see that the compute model runs when the inputs are changed.