Skip to content

Commit fd43606

Browse files
committed
update: 增加组件间通讯示例
1 parent 770e381 commit fd43606

7 files changed

Lines changed: 140 additions & 1 deletion

File tree

‎pages.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@
303303
"navigationBarTitleText": "问题反馈"
304304
}
305305
}
306-
],
306+
],
307307
"subPackages": [{
308308
"root": "pages/API",
309309
"pages": [{
@@ -875,6 +875,12 @@
875875
"navigationBarTitleText": "默认导航栏"
876876
}
877877
},
878+
{
879+
"path" : "component-communication/component-communication",
880+
"style" : {
881+
"navigationBarTitleText": "组件通讯"
882+
}
883+
},
878884
// #ifdef APP-PLUS || H5 || MP-ALIPAY
879885
{
880886
"path": "nav-transparent/nav-transparent",

‎pages/tabBar/template/template.nvue‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
name: '顶部选项卡',
7373
url: 'tabbar'
7474
},
75+
{
76+
name: '组件通讯',
77+
url: 'component-communication'
78+
},
7579
// #ifndef MP-QQ
7680
{
7781
name: 'uCharts 图表',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Vue from 'vue'
2+
3+
export default new Vue()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<template>
2+
<view>
3+
<page-head title="组件通讯示例"></page-head>
4+
<view class="uni-padding-wrap">
5+
<reciver></reciver>
6+
<sender></sender>
7+
<sender-bus></sender-bus>
8+
</view>
9+
</view>
10+
</template>
11+
12+
<script>
13+
import reciver from './reciver.vue'
14+
import sender from './sender.vue'
15+
import senderBus from './sender-bus.vue'
16+
export default {
17+
components:{
18+
reciver,
19+
sender,
20+
senderBus
21+
},
22+
data() {
23+
return {
24+
25+
}
26+
},
27+
methods: {
28+
29+
}
30+
}
31+
</script>
32+
33+
<style>
34+
.uni-padding-wrap{
35+
width: auto;
36+
padding: 20px 30px;
37+
}
38+
</style>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<template>
2+
<view>
3+
<view class="reciver">
4+
{{msg===''?'等待发送':'收到消息:'}}{{msg}}
5+
</view>
6+
</view>
7+
</template>
8+
9+
<script>
10+
import bus from './bus.js'
11+
export default {
12+
data() {
13+
return {
14+
msg: ''
15+
}
16+
},
17+
created() {
18+
uni.$on('cc', this.recive)
19+
bus.$on('cc', this.recive)
20+
},
21+
beforeDestroy() {
22+
uni.$off('cc',this.recive)
23+
bus.$off('cc', this.recive)
24+
},
25+
methods: {
26+
recive(e) {
27+
this.msg = e.msg
28+
}
29+
}
30+
}
31+
</script>
32+
33+
<style>
34+
.reciver {
35+
padding: 40px 0px;
36+
text-align: center;
37+
line-height: 40px;
38+
}
39+
</style>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<view class="sender-container">
3+
<button type="primary" @click="send">自定义EventBus</button>
4+
</view>
5+
</template>
6+
7+
<script>
8+
import bus from './bus.js'
9+
export default {
10+
methods: {
11+
send() {
12+
let num = parseInt(Math.random() * 10000)
13+
bus.$emit('cc', {
14+
msg: 'From event bus -> ' + num
15+
})
16+
}
17+
}
18+
}
19+
</script>
20+
21+
<style>
22+
.sender-container{
23+
padding: 20px;
24+
}
25+
</style>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<view class="sender-container">
3+
<button type="primary" @click="send">点击发送消息</button>
4+
</view>
5+
</template>
6+
7+
<script>
8+
export default {
9+
methods: {
10+
send() {
11+
let num = parseInt(Math.random() * 10000)
12+
uni.$emit('cc', {
13+
msg: 'From uni.$emit -> ' + num
14+
})
15+
}
16+
}
17+
}
18+
</script>
19+
20+
<style>
21+
.sender-container{
22+
padding: 20px;
23+
}
24+
</style>

0 commit comments

Comments
 (0)