请教Vue.js,加减按钮问题。

i一身正气、 2018-07-20 05:20:06
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],上百度搜过资料,纠结一下午了,还是不懂,求帮忙看看,解答一下,万分感谢。
...全文
388 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
i一身正气、 2018-07-21
  • 打赏
  • 举报
回复
不好意思 ,今天才看到,刚刚看了您的解答,理解了1和2是自定义算法类型的,谢谢了
漠子凉 2018-07-20
  • 打赏
  • 举报
回复
1和2只是用来自定义算法类型,
这里的话
1表示减法
2表示加法
那个[1],[2]给数组完全是多余的,完全可以写成1,2
当然
[1] == 1确实是返回true
[2] == 1确实是返回false

87,921

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧