Skip to content

Commit 4763c26

Browse files
committed
update
1 parent ef92dfe commit 4763c26

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/html">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>vuejs练习</title>
6+
7+
</head>
8+
<body>
9+
<!-- 子组件模板 -->
10+
<template id="child-template">
11+
<input v-model="msg">
12+
<button v-on:click="notify">Dispatch Event</button>
13+
</template>
14+
15+
<!-- 父组件模板 -->
16+
<div id="events-example">
17+
<p>Messages: {{ messages | json }}</p>
18+
<child></child>
19+
</div>
20+
<script src='http://cdn.jsdelivr.net/vue/1.0.26/vue.min.js'></script>
21+
<script src="index.js"></script>
22+
</body>
23+
</html>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// 注册子组件
2+
// 将当前消息派发出去
3+
Vue.component('child', {
4+
template: '#child-template',
5+
data: function () {
6+
return { msg: 'hello' }
7+
},
8+
methods: {
9+
notify: function () {
10+
if (this.msg.trim()) {
11+
this.$dispatch('child-msg', this.msg)
12+
this.msg = ''
13+
}
14+
}
15+
}
16+
})
17+
18+
// 初始化父组件
19+
// 将收到消息时将事件推入一个数组
20+
var parent = new Vue({
21+
el: '#events-example',
22+
data: {
23+
messages: []
24+
},
25+
// 在创建实例时 `events` 选项简单地调用 `$on`
26+
events: {
27+
'child-msg': function (msg) {
28+
// 事件回调内的 `this` 自动绑定到注册它的实例上
29+
this.messages.push(msg)
30+
}
31+
}
32+
})

0 commit comments

Comments
 (0)