在写微信小程序的时候我们有的时候会遇到一些父页面调用子组件的方法
这个时候我们就要用到一个属性了this.selectComponent()
这个方法使用注意事项
1.引入的组件要加上id如:
<selectBox id='selectBox' bind:traCheckedNum="checkNum"
selectData="{{position}}"
styleclass="{{styleclass}}">
</selectBox>
这里在扩展点知识 selectData styleclass是父组件传递给子的值组件内要声明好值得类型
bind:traCheckedNum是子组件调用页面中的方法的声明
properties: {
selectData: { //下拉列表的数据
type: Array,
value: [] //初始数据,可通过属性修改
},
styleclass:{
type: String,
value: [] //初始数据,可通过属性修改
}
},
2.确保组件的外围标签有wx:if的时候 其值一定要为true.否则this.selectComponent()获取不到值返回null
3.返回值的使用如下方代码
因此组件在wx:if的包裹中所以我们要判断值为true时才能执行代码
if (!this.data.checkNumclass && this.data.is_overdue ==3){
console.log(this.selectComponent("#selectBox"))
this.selectComponent("#selectBox").setData({
index:-1
})
}
this.selectComponent("#selectBox") 返回属性
如我们要修改组件内的值就要调用setData,使用方法是
this.selectComponent("#selectBox").setData({
index:-1
})
调用自定义方法 this.selectComponent("#selectBox").selectTap(),
selectTap为子组件methods中的方法