-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-03.html
51 lines (49 loc) · 1.02 KB
/
example-03.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
51
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>实例事件</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="app">
{{count}}
<div><br></div>
<div><button v-on:click="add">add</button></div>
<div><br></div>
</div>
<div><button onclick="reduce()">reduce</button></div>
<div><button onclick="reduceOnce()">reduceOnce</button></div>
<div><button onclick="off()">$off事件</button></div>
</body>
<script>
var app=new Vue({
el:'#app',
data:{
count:0
},
methods:{
add:function () {
this.count++
}
}
})
app.$on('reduce',function () {
console.log('执行了reduce方法')
this.count--
})
function reduce() {
app.$emit('reduce')
}
app.$once('reduceOnce',function () {
console.log('执只能调用一次')
this.count--
})
function reduceOnce() {
app.$emit('reduceOnce')
}
function off() {
app.$off('reduce')
}
</script>
</html>