1. 首先定义父组件和子组件
父组件:这里使用Pages里的
2. 在父组件中定义方法向子组件发送命令
这里我在onShow()中向子组件发送命令。直接在onshow中通过this.selectComponet()实例化子组件,然后调用子组件方法向父组件发送信息。
子组件中定义方法:
//向父page index传递参数
retransparam(){
const data = {
year:this.data.curYear,
month:this.data.curMonth
}
this.triggerEvent('customEvent2',data)
console.log('oooooooooooo')
},
使用this.triggerEvent()
在父组件wxml中引入子组件:
<democalendar bind:customEvent2="get_data_from_child" ></democalendar>
在onShow()中实例化子组件并调用retransparam()方法向父组件发送data。通过父组件的get_data_from_child函数来接收data。
//从子组件calendar中获取参数
get_data_from_child(e){
console.log(e)
}
{type: "customEvent2", timeStamp: 184638, target: {…}, currentTarget: {…}, mark: {…}, …}
本文介绍了如何在React或类似框架中创建父组件与子组件的交互,通过定义方法和事件触发,展示如何在父组件向子组件发送命令并通过`triggerEvent`实现数据传递,以及在子组件中接收和处理这些数据。
1011

被折叠的 条评论
为什么被折叠?



