请教Vue.js,加减按钮问题。
html:
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
<div id="app">
<div id="counter-event-example">
<p>{{ total }}</p>
<button-counter v-on:increment="incrementTotal"></button-counter><br/>
<button-counter v-on:increment="incrementTotal"></button-counter>
</div>
</div>
Script:
Vue.component('button-counter', {
template: '<div><button v-on:click="incrementHandler(1)">-</button>{{ counter }}<button v-on:click="incrementHandler(2)">+</button></div>',
data: function () {
return {
counter: 0
}
},
methods: {
incrementHandler: function (v) {
if(v==1){
this.counter -= 1
this.$emit('increment',[1])
}else{
this.counter += 1
this.$emit('increment',[2])
}
}
},
})
new Vue({
el: '#counter-event-example',
data: {
total: 0
},
methods: {
incrementTotal: function (d) {
if(d==1){
this.total -= 1
}else{
this.total += 1
}
}
}
})
本人前端小白,现在在自学WEB vue,JS也不扎实,这里不明白 自定义组件中,
incrementHandler: function (v) {
if(v==1){
this.counter -= 1
this.$emit('increment',[1])
}else{
this.counter += 1
this.$emit('increment',[2]) 这一段 中的[1],[2] 还有条件为什么有用v ==1 ,不是点击一下么,理解$触发increment对象,但不明白后面的[1],[2],上百度搜过资料,纠结一下午了,还是不懂,求帮忙看看,解答一下,万分感谢。