官方文档:创建自定义组件
在ArkUI
中,UI显示的内容均为组件,由框架直接提供的称为系统组件
,由开发者定义的称为自定义组件
。
目录标题
如何自定义组件?
1・struct + 自定义组件名 + {…}
struct MyComponent {
}
2・build()函数:
build()
函数用于定义自定义组件的声明式UI描述
,自定义组件必须定义build()
函数。
struct MyComponent {
build() {
}
}
3・@Component
struct
被@Component
装饰后具备组件化的能力。@Component
装饰器仅能装饰struct
关键字声明的数据结构。
@Component
struct MyComponent {
}
4・@Entry
@Entry
装饰的自定义组件将作为UI页面的入口。
@Entry
@Component
struct MyComponent {
}
5・@Reusable
@Reusable
装饰的自定义组件具备可复用能力
@Reusable
@Component
struct MyComponent {
}