uni-app父子通讯,跨页面跨组件通讯
Posted 曹豆芽
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uni-app父子通讯,跨页面跨组件通讯相关的知识,希望对你有一定的参考价值。
今天豆芽在复习uni-app也是复习了下uni-app父子通讯,跨页面、跨组件通讯.
uni-app父子通讯
父向子通讯
//index.vue
<template>
<view class="content">
<testa :dataStr="dataStr"></testa>
</view>
</template>
<script>
import testa from '../../components/testa.vue'
export default
data()
return
dataStr:'好东西'
,
components:
testa
,
onLoad() ,
methods:
</script>
<style>
</style>
//testa.vue
<template>
<view>
豆芽给的dataStr
</view>
</template>
<script>
export default
data()
return
,
methods:
,
props:
dataStr:
type:String
</script>
<style>
</style>
子传父通讯
//testa.vue
<template>
<view>
豆芽给的dataStr
</view>
</template>
<script>
export default
data()
return
,
methods:
,
created()
this.$emit('data','豆芽儿子给的')
,
props:
dataStr:
type:String
</script>
<style>
</style>
//index.vue
<template>
<view class="content">
<testa :dataStr="dataStr" @data="data"></testa>
</view>
</template>
<script>
import testa from '../../components/testa.vue'
export default
data()
return
dataStr:'好东西',
resData:""
,
components:
testa
,
onLoad() ,
methods:
data(res)
console.log(res)
</script>
<style>
</style>
uni-app跨页面、跨组件通讯
//index.vue
<template>
<view class="content">
<testa></testa>
<testb></testb>
</view>
</template>
<script>
import testa from '../../components/testa.vue'
import testb from '../../components/testb.vue'
export default
data()
return
userList:
,
components:
testa,
testb
,
onLoad() ,
methods:
addNum()
uni.$emit('updateNum', 10)
</script>
<style>
</style>
//testa.vue
<template>
<view>
这是a组件 :<button @click="addNum">修改b组件的数据num</button>
</view>
</template>
<script>
export default
data()
return
num:0
,
methods:
addNum()
uni.$emit('updateNum',10)
</script>
<style>
</style>
//testb.vue
<template>
<view>
b新建的数据num
</view>
</template>
<script>
export default
data()
return
num:0
,
created()
uni.$on('updateNum',(num)=>
this.num += num;
)
</script>
<style>
</style>
🙏🏿🙏🏿🙏🏿🙏🏿
以上是关于uni-app父子通讯,跨页面跨组件通讯的主要内容,如果未能解决你的问题,请参考以下文章
159微信小程序跨页面跨组件同步全局状态跨页面跨组件通讯方案,使用自制广播模块实现