这一节主要了解一下鸿蒙中自定义点击事件,主要是实现在父组件点击子组件后获取子组件的返回参数。
栗子:
import { MyCustomButton } from './MyCustomButton';@Entry
@Component
struct Index {@State message: string = 'Hello World';build() {Column(){MyCustomButton({onPlayClick:(res:string) => {console.log(" sub child =>"+res)}})}.height('100%').width('100%')}
@Component
export struct MyCustomButton {@State customParam: string = "Hello, World!";onPlayClick?:(res:string) => voidbuild() {Column(){Button(this.customParam).width('100%').height('80px').margin({top:20}).backgroundColor('#007BFF').fontColor('#FFFFFF').margin({ top: '20px' }).onClick(() => {if(this.onPlayClick !== undefined) {this.customParam = "开始飞翔..."this.onPlayClick(this.customParam)}});}.width('100%').height('100%')}
}
输出: