vue bus

vue 基础知识和用法

效果

vue

源码

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
<html>
<header>
<title>Vue.js bus</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</header>

<body>
<div id="box">
<h2>Author</h2>
<author></author>
<h2>Subscriber</h2>
<describe></describe>

</div>
</body>

<script>
var bus = new Vue()

Vue.component("author", {
template: `<div>
<input type="text" ref="mytext">
<button @click="handlerClick">发布</button>
</div>`
,
methods: {
handlerClick() {
console.log("发布文章", this.$refs.mytext.value)
bus.$emit("t-message", this.$refs.mytext.value)
}
}
})

Vue.component('describe', {
template: `<div style="background:red;width=200px">
订阅者
</div>`
,
mounted() {
bus.$on("t-message", (data) => {
console.log("收到文章", data)
})
}
})

var vm = new Vue({
el: "#box",
data: {
},
methods: {

}
})


</script>

</html>