-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy path3-2 Custom Events.html
192 lines (183 loc) · 5.71 KB
/
3-2 Custom Events.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="vue.js"></script>
<meta charset="UTF-8">
<title>vue.js</title>
<style>
.component3 {
margin: 10px 10px 10px 100px;
display: block;
background-color: #ffcc66;
}
.component2 {
background-color: yellow;
padding: 10px;
margin: 10px;
}
</style>
</head>
<body>
<!-- 最小的组件模板-->
<template id="component3">
<div class="component3">
<br> 组件 c3
<input v-model="msg">
<button @click="c3_do1">c3 广播事件至根组件</button>
<button @click="c3_do2">c3 $parent 设定资料</button>
<button @click="c3_do3">c3 $root 设定资料</button>
<button @click="c3_do4">c3 all_events 事件</button>
<button @click="c3_do5">c3 all_events1事件</button>
</div>
</template>
<!-- 子组件模板 -->
<template id="component2">
<div class="component2">
<p>组件 c2 msg: {{ msg | json }}</p>
<input v-model="msg">
<button v-on:click="c2_do1">c2 广播事件至根组件</button>
<button v-on:click="c2_do2">c2 清空 $parent 资料</button>
<component3></component3>
</div>
</template>
<!-- 根组件模板 -->
<div id="component1">
<hr>
<p>根组件 Messages: {{ messages | json }}</p>
<button v-on:click="c1_do1">组件 c2[0] 设值 子组件的资料 </button>
<button v-on:click="c1_do2">组件 c2[1] 设值 子组件的资料 </button>
<button v-on:click="c1_do3">组件 c2[2] 设值 子组件的资料 </button>
<c2></c2>
<c2></c2>
<c2></c2>
</div>
<div id="app">
<my-component></my-component>
</div>
<script>
Vue.component('component3', {
template: '#component3',
data: function() {
return {
msg: 'c3 台湾小凡',
}
},
methods: {
'c3_do1': function() {
if (this.msg.trim()) {
this.$dispatch('c1_events', this.msg)
this.msg = ''
}
},
'c3_do2': function() {
this.$parent.$set('msg', "设定子组件完成");
},
'c3_do3': function() {
this.$root.messages.push(this.msg);
this.msg = '';
},
'c3_do4': function() {
this.$dispatch('all_events', this.msg);
},
'c3_do5': function() {
this.$dispatch('all_events1', this.msg);
}
}
});
// 注册子组件
// 将当前消息派发出去
Vue.component('c2', {
template: '#component2',
data: function() {
return {
msg: 'c2 感谢 群友'
}
},
methods: {
'c2_do1': function() {
if (this.msg.trim()) {
this.$dispatch('c1_events', this.msg)
this.msg = ''
}
},
'c2_do2': function() {
this.$parent.messages = [];
}
},
events: {
'all_events': function(msg) {
console.log('all_events c2:' + msg);
return true;
},
'all_events1': function(msg) {
console.log('all_events1 c2:' + msg);
}
}
});
// 初始化父组件
// 将收到消息时将事件推入一个数组
var Parent = new Vue({
el: '#component1',
data: {
messages: []
},
// 在创建实例时 `events` 选项简单地调用 `$on`
events: {
'c1_events': function(msg) {
// 事件回调内的 `this` 自动绑定到注册它的实例上
this.messages.push(msg)
console.log('c1_events:' + msg);
},
'all_events': function(msg) {
console.log('all_events Praent:' + msg);
},
'all_events1': function(msg) {
console.log('all_events1 Praent:' + msg);
}
},
methods: {
'c1_do1': function() {
this.$emit('c1_events', 'Parent events do')
this.$children[0].msg = '组件c2[0]设值';
},
'c1_do2': function() {
this.$children[1].msg = '组件c2[1]设值';
},
'c1_do3': function() {
this.$children[2].msg = '组件c2[2]设值';
}
}
});
var vm = new Vue({
el: '#app',
components: {
'my-component': Parent
},
ready() {
this.$on('events3', function(msg) {
console.log(msg);
});
},
events: {
'events4': function(msg) {
console.log(msg);
}
}
});
vm.$on('events1', function(msg) {
console.log(msg);
});
vm.$once('events1-once', function(msg) {
console.log('once:' + msg);
});
vm.$emit('events1', 'events1 do 1');
vm.$emit('events1', 'events1 do 2');
vm.$emit('events1-once', 'events1-once do1');
vm.$emit('events1-once', 'events1-once do2');
vm.$emit('events3', 'events3 do 1');
vm.$emit('events3', 'events3 do 2');
vm.$emit('events4', 'events4 do 1');
vm.$emit('events4', 'events4 do 2');
</script>
</body>
</html>