forked from sunseekers/Vue2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus.html
65 lines (63 loc) · 1.53 KB
/
bus.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>子组件之间传递数据</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<script type="text/javascript" src='js/vue.min.js'></script>
</head>
<body>
<div id='app'>
<foo></foo>
<hr>
<bar></bar>
</div>
<script type="text/javascript">
//利用 eventBus 作为一个中间中转站,去实现兄弟之间的通信
var eventBus=new Vue({});
var foo={
template:`<button @click='addBar'>{{fooCount}}</button>`,
data(){
return {fooCount:0}
},
methods:{
addBar(){
eventBus.$emit('addBar')
}
},
mounted(){
//再次借用了eventBus
eventBus.$on('addFoo',function(){
this.fooCount++
}.bind(this))
}
}
var bar={
template:`<button @click='addFoo'>{{barCount}}</button>`,
data(){
return {barCount:0}
},
methods:{
addFoo(){
console.log(eventBus);
eventBus.$emit('addFoo')
}
},
mounted(){
eventBus.$on('addBar',function(){
this.barCount++
}.bind(this))
}
}
new Vue({
el:'#app',
components:{
foo,bar
}
})
</script>
</body>
</html>