forked from sunseekers/Vue2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emit.html
47 lines (47 loc) · 1.07 KB
/
emit.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>emit事件</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
<div class='emit'>
<p>{{ total }}</p>
<button-counter @increment='incrementTotal'></button-counter>
<button-counter @increment='incrementTotal'></button-counter>
</div>
<script type="text/javascript" src='js/vue.min.js'></script>
<script type="text/javascript">
new Vue({
el:'.emit',
data:{
total:0
},
components:{
'button-counter':{
template:"<button @click='incrementCounter'>{{ counter}}</button>",
/* 在组件的定义中 data 只接受 function*/
data(){
return {counter:0}
},
methods:{
incrementCounter(){
this.counter++;
this.$emit('increment');//触发increment事件
}
}
}
},
methods:{
incrementTotal(){
this.total++;
}
}
});
</script>
</body>
</html>