@Component
struct Child {
@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
@Component
struct Parent {
// ChildRef = new ChildController()
@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')
})
}
}
}
第二种:eventHub 也可以 类似安卓的事件总线