-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31.组件通信4——使用v-on绑定自定义事件.html
50 lines (48 loc) · 1.26 KB
/
31.组件通信4——使用v-on绑定自定义事件.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!--
讲解:
本例中,点击button > 触发click事件 > click事件调用 increment函数
> increment事件(自定义)调用incrementTotal函数
可以看出,increment既是事件 又是函数
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自定义事件 </title>
<script type="text/javascript"src="vue.js"></script>
</head>
<body>
<div id="counter-event-example">
<p>{{ total }}</p>
<button-counter v-on:increment="incrementTotal"></button-counter>
<button-counter v-on:increment="incrementTotal"></button-counter>
</div>
</body>
<script type="text/javascript">
Vue.component('button-counter', {
template: '<button v-on:click="increment">{{ counter }}</button>',
data: function () {
return {
counter: 0
}
},
methods: {
increment: function () {
this.counter += 1
this.$emit('increment')
}
},
})
new Vue({
el: '#counter-event-example',
data: {
total: 0
},
methods: {
incrementTotal: function () {
this.total += 1
}
}
})
</script>
</html>