HarmonyOS 鸿蒙Next 在子组件写了一个方法,在父组件想直接调用这个方法,该如何实现呢?
HarmonyOS 鸿蒙Next 在子组件写了一个方法,在父组件想直接调用这个方法,该如何实现呢?
在子组件写了一个方法,在父组件想直接调用这个方法,该如何实现呢?
3 回复
参考:
[@Component](/user/Component)
struct Child {
[@State](/user/State) private text: string = '初始值'
private controller: ChildController = new ChildController();
aboutToAppear() {
if (this.controller) {
this.controller.changeText = this.changeText
}
console.log('aaa')
}
private changeText = (value: string) => {
this.text = value
console.log('bbb')
}
build() {
Column() {
Text(this.text)
}
}
}
class ChildController {
changeText = (value: string) => {
console.log('11111')
}
}
export let ChildRef = new ChildController()
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Parent {
// ChildRef = new ChildController()
[@State](/user/State) noShow: boolean = false
build() {
Column() {
Text('获取Child的exposeMethods!').fontSize('18vp').fontColor(Color.Gray)
Divider()
Child({ controller: ChildRef })
Child()
Button('Parent调用childer的changeText').onClick(() => {
ChildRef.changeText('Parent调用childer的changeText')
})
}
}
}
更多关于HarmonyOS 鸿蒙Next 在子组件写了一个方法,在父组件想直接调用这个方法,该如何实现呢?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html