Home>

I want to make a simple calculator.
Which one is better for watch or computed when I want to calculate multiple calculation results for one value?

The following is a simple method to detect changes in numbers, execute processing, and output the results.

Value:<input type = "number" v-model = "val">
    <table>
      <tr><th>add:</th><td>{{add}}</td></tr>
      <tr><th>sub:</th><td>{{sub}}</td></tr>
      <tr><th>multiiple:</th><td>{{mult}}</td></tr>
      <tr><th>divide:</th><td>{{div}}</td></tr>
    </table>
watch: {
  val: function (value) {
    this.val = value;
    // parseInt
    // function to convert string to integer
    var val = parseInt (value);
    this.val = parseInt (value);
    this.add = Math.floor (val + 2);
    this.sub = Math.floor (val-2);
    this.mult = Math.floor (val * 2);
    this.div = Math.floor (val/2);
  }
},
I want to achieve it with computed

watch made it easy.
But can this be done with computed? I tried and tried various things, but I couldn't.
Please let me know if it is feasible.

If i can't or don't like it, please let us know why.