简体   繁体   中英

Counter not increment in vue.js

I have a button and added a click event on which data value increment by 5 but it is appended by 5

https://jsfiddle.net/neyaz90/dkvmmrbd/

<div id="react">
<button @click='counter += 5'>Increment</button>
<p>{{result}}</p>

new Vue({
el:'#react',
data:{
counter:'0'
},
computed:{
  result:function(){
    return this.counter;
 }
}
});

please help in this.

You need to use a Number instead of a String for 0 See jsfiddle here.

HTML

<div id="react">
  <button @click="counter += 5">Increment</button>
  <p>{{ result }}</p>
</div>

JS

new Vue({
  el: '#react',
  data: {
    counter: 0
  },
  computed: {
    result: function() {
      return this.counter;
    }
  }
})

counter is defined with '0'(String) instead of 0(number).

You also don't need the computed value to show the result.

Only {{counter}} would be enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM