小程序子组件向父组件传递数据
绑定点击事件, 子组件触发父组件的自定义事件 this.triggerEvent("事件名",参数)向父组件传递参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //子组件js Component({ properties: { test_data:{ type:Array, value:[] } }, methods:{ handleTap(e) { const index = e.currentTarget.dataset.index; this .triggerEvent( "handlefatherEvent" ,index); } } }) |
1 2 3 | <!-- 父组件wxml --> < child test_data="{{testData}}" bindhandleFatherEvent="handleFatherEvent"> </ child > |
父组件绑定自定义事件,接收子组件传递数据
0,apple,isActive:1,banana,isActive:2,grape,isActive:== =>i===index? v.isActive=:v.isActive=
1 2 3 4 5 6 7 8 | <!- 子组件wxml --> < view class="child"> < view class="child_item {{item.isActive===true?'active':''}}" wx:for="test_data" wx:key="id" bindtap="handleTap"<br> data-index = {{index}} >{{item.name}}</ view > </ view > |