当前位置: 首页> 汽车> 新车 > 鸿蒙父组件中如何处理子组件内点击事件

鸿蒙父组件中如何处理子组件内点击事件

时间:2025/8/29 0:25:06来源:https://blog.csdn.net/qq_36859035/article/details/140849235 浏览次数: 0次

在父组件中初始化子组件时,将父组件中定义的方法,传递给子组件,在子组件中调用该方法,类似于变量传递。参考代码如下:

class Model { value: string = '' 
} @Entry 
@Component 
struct EntryComponent { test() { console.log('testTag test in my component'); } build() { Column() { MyComponent({ title: { value: 'Hello World 2' }, count: 7, click: this.test }) //初始化时传递定义的方法 } } 
} @Component 
struct MyComponent { @State title: Model = { value: 'Hello World' } @State count: number = 0 click: () => void = () => { }; private toggle: string = 'Hello World' private increaseBy: number = 1 build() { Column() { Text(`${this.title.value}`).fontSize(30) Button(`Click to increase count=${this.count}`) .margin(20) .onClick(() => { // 修改内部状态变量count this.count += this.increaseBy this.click(); }) } } 
}
关键字:鸿蒙父组件中如何处理子组件内点击事件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: