小程序父子组件通信

时间:2021-01-30 17:54:24   收藏:0   阅读:17

父组件向子组件传递数据

{
  "usingComponents": {
  	//"Tab" 是自己定义的组件名称
  	//"../../components/Tab/Tab" 是组件的路径
    "Tab":"../../components/Tab/Tab"
  }
}
<Tab tabs="{{tabs}}" data-index={{index}}></Tab>
Component({
  ///组件的属性列表
  properties: {
    tabs:{
      type:Array, //要接收的数据类型
      value:[] //默认值(可选)
    }
  }
})

子组件向父组件传递数据

<button size="mini" bind:tap="handleTap">+1</button>
methods: {
    handleTap(){
      //三个参数:方法名称,子组件要往父组件传递的参数
      this.triggerEvent("increment",{index:0})
    }
  }
<Tab tabs="{{tabs}}" bindincrement="handleIncrement"></Tab>
handleIncrement(e){
	//子组件向父组件传递过来的参数都储存在事件对象e中
    const index = e.detail.index
    this.setData({
      count:this.data.count + index
    })
}

原文:https://www.cnblogs.com/jincanyu/p/14349236.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!