🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 1. 子组件是模板文件 向父组件传参 ### 1. 先在子组件的wxml中绑定一个点击事件 bind:tap="onLike" ~~~ <view class="container"> <image src="{{isLike?yesSrc:noSrc}}" class="icon" bind:tap="onLike"></image> <text>{{count}}</text> </view> ~~~ ### 2. 在子组件的js的methods的方法中处理事件 使用triggerEvent方法向父组件传参 ~~~ methods: { onLike(){ var isLike = this.properties.isLike; var count = this.properties.count; if(isLike){ this.setData({ isLike:false, count:count-1 }) }else{ this.setData({ isLike:true, count:count+1 }) } let behavior = this.properties.isLike?"like":"like/cancel"; /* 在子组件中自定义事件,向组父组件传递参数 */ this.triggerEvent("wang",{behavior:behavior}); } } ~~~ ### 3. 在父组件的wxml中绑定一个方法名为 triggerEvent定义的方法名 的事件 ~~~ <v-like isLike="{{classic.like_status}}" count="{{classic.fav_nums}}" bind:wang="onLike"></v-like> ~~~ ### 4. 然后再js中处理事件得到参数 ~~~ Page({ onLoad(){ wx.request({ url: 'http://bl.7yue.pro/v1/classic/latest', header: { 'Content-Type': 'application/json', "appkey":"9xK0CrzQREI1yGPh" }, success: res=>{ this.setData({ classic:res.data }) } }) }, onLike(e){ console.log(e.detail) } }) ~~~