微信小程序—组件通信—使用select***ponent获取组件实例
子组件***ponent
wxml
<view>{{count}}</view>
js
properties: {
count:Number
},
methods: {
addCount(){
this.setData({
count:this.properties.count+1
})
this.triggerEvent('sync',{value:this.properties.count})
}
}
一.通过父页面增加子组件的数据值
父页面page
wxml
<mytest count="{{count}}" bind:sync="syn***ount" class="childC" id="***"></mytest>
<view>{{count}}</view>
<button bindtap="getChild">获取子组件的实例对象</button>
js
data: {
count:1
},
syn***ount(e){
this.setData({
count:e.detail.value
})
},
getChild(){
const child = this.select***ponent('.childC')
child.setData({
count:child.properties.count+1
})
},
二.通过父页面调用子组件的方法
getChild(){
const child = this.select***ponent('.childC')
child.addCount()
},