微信小程序—组件通信—使用selectComponent获取组件实例

子组件component
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="syncCount" class="childC" id="cC"></mytest><view>{{count}}</view><button bindtap="getChild">获取子组件的实例对象</button>

js

 data: {count:1}, syncCount(e){this.setData({count:e.detail.value})},getChild(){const child = this.selectComponent('.childC')child.setData({count:child.properties.count+1})},

二.通过父页面调用子组件的方法

getChild(){const child = this.selectComponent('.childC')child.addCount()},