今天我們講解一下微信小程序子組件如何給父組件傳參數,子組件如何調用父組件的函數
參考文章: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:'吃飯'} },