@Event装饰回调方法,用于状态管理V2中,作为自定义组件的输出。@Event通常与@Param配合使用,@Param负责由父组件向子组件传递数据,@Event负责定义子组件向父组件传递消息的回调接口,适用于需要在子组件中触发父组件状态变更或事件处理的场景。
开发指南参考:@Event:规范组件输出。
从API version 12开始,支持该装饰器。
const Event: PropertyDecorator
模型约束: 此接口仅可在Stage模型下使用。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
卡片能力: 从API version 23开始,该接口支持在ArkTS卡片中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
示例:
@Entry
@ComponentV2
struct Index {
@Local name: string = 'Tom';
build() {
Column() {
Child({
name: this.name,
changeFactory: (type: number) => {
// @Event装饰的函数,在实现中修改父组件中的状态变量
if (type == 1) {
this.name = 'Tom';
} else if (type == 2) {
this.name = 'Jerry';
}
}
})
}
}
}
@ComponentV2
struct Child {
@Param name: string = '';
// @Event装饰函数,用于向父组件传递消息
@Event changeFactory: (type: number) => void = (type: number) => {};
build() {
Column() {
Text(`name: ${this.name}`)
Button('change to Tom')
.onClick(() => {
this.changeFactory(1);
})
Button('change to Jerry')
.onClick(() => {
this.changeFactory(2);
})
}
}
}