1.父子传值,父组件通过属性的方式去给子组件传递值,子组件在properties属性去接收父组件传递过来的值:
父组件部分:
<view class="pcolor">
<customer id="child" bind:changSex="changSex" sex="{{sex}}" checked="{{checked}}"></customer>
<view>父组件的值:{{sex}}</view>
</view>
子组件部分:
properties: {
sex:{
type:String,
value:'女'
},
checked:{
type:Boolean,
value:false
}
},
通过 this.triggerEvent('changSex','女')去修改父组件的值
2.组件之间的传值:
需要使用pubsub-js这个插件,下载npm install pubsub-js
在组件1 引入import PubSup from 'pubsub-js', 通过PubSup.publish('info',{name:'zhansgan',age:20});去传递值
在组建2 引入import PubSup from 'pubsub-js',在生命周期去接收
lifetimes:{
attached() {
PubSup.subscribe('info',(name,value)=>{
console.log(name,value)
})
}
},
3.页面间的传值:
跳转页面:
jump:function() {
wx.navigateTo({
url: '/pages/list/list',
events:{
send1:(e)=>{
console.log(e)
}
},
success:(res)=>{
console.log(res)
res.eventChannel.emit('send',{name:'zhangsan'})
}
})
},
接收页面:
onLoad() {
const openerEventChannel = this.getOpenerEventChannel();
console.log('111')
openerEventChannel.on('send',(res)=>{
console.log(res)
})
openerEventChannel.emit('send1',{age:10})
}