-
Notifications
You must be signed in to change notification settings - Fork 0
/
5非父子组件通信.html
56 lines (52 loc) · 1.04 KB
/
5非父子组件通信.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="vue.js"></script>
</head>
<body>
<div id="app">
<component-a></component-a>
<component-b></component-b>
</div>
<script>
var vm = new Vue();
var A = {
template: '<button @click="eventa">{{a}}</button>',
data: function(){
return {
a: 'A组件内容'
}
},
methods:{
eventa:function(){
vm.$emit('change',this.a)
}
}
}
var B = {
template: '<button>{{a}}</button>',
data: function(){
return {
a: 'B组件内容'
}
},
mounted() {
vm.$on('change',function(data){
this.a = data
}.bind(this))
}
}
new Vue({
el: '#app',
components:{
'component-a': A,
'component-b': B
}
})
</script>
</body>
</html>