微信小程序子组件给父组件传参,调用父组件函数


今天我们讲解一下微信小程序子组件如何给父组件传参数,子组件如何调用父组件的函数

参考文章:http://www.freetechs.cn/archives/125

1、子组件js文件定义函数addInfo

注:triggerEvent第一参数要跟第4步中bind:addInfo中的字符保持一致

addInfo(){
      let item = {title:'测试',money:8,category:'吃饭'}//要传给父组件的参数
      this.triggerEvent('addInfo',item)//通过triggerEvent将参数传给父组件
    },

 

2、子组件wxml绑定函数addInfo

<button type="primary" bindtap="addInfo">确定</button>

 

3、父组件.json文件引用子组件(这段代码只是示例)

{
    "component": true,
    "usingComponents": {
        "add-item": "../demo/add"
    }
}

4、父组件绑定自定义点击函数

<add-item nowAddDate="{{isShowAdd}}" bind:addInfo="getAddInfo"></add-item>

5、父组件接收子组件传过来的参数

getAddInfo(e){
    console.log(e.detail)//{title:'测试',money:8,category:'吃饭'} 
},

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM