首页 > 微信 > 详细

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

时间:2020-05-12 14:55:06      阅读:134      评论:0      收藏:0      [点我收藏+]

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

参考文章: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:‘吃饭‘} 
},

 

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

原文:https://www.cnblogs.com/sjns/p/12876070.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!