鸿蒙HarmonyOS ArkTS父组件调用子组件方法,父组件刷新时需要调用子组件的请求方法:
1、子组件ChildCom创建一个ChildController
class ChildController {
refreshData = () => {}
}
2、子组件声明controller属性
private controller: ChildController = new ChildController();
3、子组件添加方法
refreshData = () => {
this.curPage = 1;
this.loadData();
}
4、子组件在aboutToAppear方法中赋值
aboutToAppear() {
if (this.controller) { //给controller对应的方法赋值
this.controller.refreshData = this.refreshData;
}
}
5、在父组件同样创建一个ChildController(当然,也可以放在公共文件,子组件和父组件都引入)
class ChildController {
refreshData = () => {}
}
6、父组件导入ChildController并声明一个响应式
private childRef = new ChildController();
7、创建子组件时给子组件的controller赋值
ChildCom({ controller: this.childRef })
8、调用时:
this.childRef.refreshData()