父组件:
<my-test1 count="{{count}}" bind:sync="syncCount" class="customA"></my-test1>
<view>父组件中,count值是{{count}}</view>
<button bindtap="getChild">获取子组件实例对象</button>
Page({
data: {
count:0
},
syncCount(e){
this.setData({
count:e.detail.value
})
},
getChild(){
const child = this.selectComponent('.customA')
child.addCount()
}
})
子组件:
<view>子组件中,count值是{{count}}</view>
<button bindtap="addCount">+1</button>
Component({
properties: {
count: Number
},
data: {
},
methods: {
addCount(){
this.setData({
count: this.data.count + 1
})
this.triggerEvent('sync',{value:this.properties.count})
}
}
})
组件引用
"usingComponents": {
"my-test1":"/components/test1/test1"
}
|