创建自定义组件
在 ArkUI 中,UI 显示的内容均为组件,由框架直接提供的称为系统组件,由开发者定义的称为自定义组件。在进行 UI 界面开发时,通常不是简单的将系统组件进行组合使用,而是需要考虑代码可复用性、业务逻辑与 UI 分离,后续版本演进等因素。因此,将 UI 和部分业务逻辑封装成自定义组件是不可或缺的能力。
自定义组件具有以下特点:
可组合: 允许开发者组合使用系统组件,及其属性和方法
可重用: 自定义组件可以被其他组件重用,并作为不同的实例 在不同的父组件或容器中使用
数据驱动 UI 更新 : 通过状态变量的改变,来驱动 UI 的刷新
自定义组件的基本用法
以下示例展示了自定义组件的基本用法。
@Component
export default struct HelloComponent {
@State message:string = "";
@State value:number = 0;
build() {
// HelloComponent自定义组件组合系统组件Row和Text
Row() {
Text(this.message)
.onClick(() => {
// 状态变量message的改变驱动UI刷新,UI从'Hello, World!'刷新为'Hello, ArkUI!'
this.message = 'Hello, ArkUI!';
})
Text(this.value.toString()).onClick(()=>{
this.value +=1;
})
}
}
}
INFO
注意如果在另外的文件中引用自定义组件
需要使用 export 关键字导出
并在使用的页面 import 该自定义组件
HelloComponent 可以在其他自定义组件中的 build()函数中多次创建,实现自定义组件的重用。
import HelloComponent from "../components/HellComponent"
interface IHelloComponentParam {
message: string;
value:number ;
}
@Entry
@Component
struct Index {
param: IHelloComponentParam = {
message: '父亲传递过来!',
value:1
}
build() {
Column() {
Text('ArkUI message')
HelloComponent(this.param);
Divider()
HelloComponent(this.param);
}
}
}
自定义组建的基本结构
struct
自定义组件基于 struct 实现,struct
+ 自定义组件名 + {...}
的组合构成自定义组件,不能有继承关系。对于 struct 的实例化,可以省略 new。
INFO
自定义组件名,类名,函数名 不能和系统组件名相同
@Component
@Component 装饰器仅能装饰 struct 关键字声明的数据结构。struct 被@Component 装饰后具备组件化的能力,需要实现 build 方法描述 UI,一个 struct 只能被一个@Component 装饰。@Component 可以接受一个可选的 bool 类型参数。
@Component
struct MyComponent {
}
freezeWhenInactive 组件冻结选项,当设置为 true 时,组件在不可见时会被冻结,从而释放资源。默认为 false。
@Component(freezeWhenInactive: true)
struct MyComponent {
}
build()函数
build()函数用于定义自定义组件的声明式 UI 描述,自定义组件必须定义 build()函数。
@Component
struct MyComponent {
build() {
}
}
@Entry
@Entry 装饰的自定义组件将作为 UI 页面的入口。在单个 UI 页面中,最多可以使用@Entry 装饰一个自定义组件。
@Entry 可以接受一个可选的 LocalStorage 的参数。
@Entry
@Component
struct MyComponent {
}
- EntryOptions 命名路由跳转选项。
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
routeName | string | 否 | 表示作为命名路由页面的名字。 |
storage | LocalStorage | 否 | 页面级的 UI 状态存储 |
useSharedStorage12 | boolean | 否 | 是否使用 LocalStorage.getShared()接口返回的 LocalStorage 实例对象,默认值 false。 |
INFO
当 useSharedStorage 设置为 true,并且 storage 又被赋值时,useSharedStorage 的值优先级更高。
@Entry({ routeName : 'myPage' })
@Component
struct MyComponent {
}
- @Reusable:@Reusable 装饰的自定义组件具备可复用能力(用的少)
@Reusable
@Component
struct MyComponent {
}
成员函数/变量
自定义组件除了必须要实现的 build()函数外,还可以实现其他成员函数,成员函数必须具有以下约束:
- 自定义组建的成员函数是私有的,并且不建议声明成静态函数
自定义组件可以包含成员变量,成员变量具有以下约束:
自定义组件的成员变量为私有的,且不建议声明为静态变量
自定义组件的成员变量本地初始化有些是可选的,有些是必选的。具体是否需要本地初始化,是否需要从父组件通过参数传递初始化子组件的成员变量
自定义组件的参数规定
从上文的示例中,我们已经了解到,可以在 build 方法里创建自定义组件,在创建自定义组件的过程中,根据装饰器的规则来初始化自定义组件的参数。
@Component
struct MyComponent {
private countDownFrom: number = 0;
private color: Color = Color.Blue;
build() {
}
}
@Entry
@Component
struct ParentComponent {
private someColor: Color = Color.Pink;
build() {
Column() {
// 创建MyComponent实例,并将创建MyComponent成员变量countDownFrom初始化为10,将成员变量color初始化为this.someColor
MyComponent({ countDownFrom: 10, color: this.someColor })
}
}
}
下面的示例代码将父组件中的函数传递给子组件,并在子组件中调用。
@Entry
@Component
struct Parent {
@State cnt: number = 0
submit: () => void = () => {
this.cnt++;
}
build() {
Column() {
Text(`${this.cnt}`)
Son({ submitArrow: this.submit })
}
}
}
@Component
struct Son {
submitArrow?: () => void
build() {
Row() {
Button('add')
.width(80)
.onClick(() => {
if (this.submitArrow) {
this.submitArrow()
}
})
}
.justifyContent(FlexAlign.SpaceBetween)
.height(56)
}
}
build()函数
所有声明在 build()函数的语句,我们统称为 UI 描述,UI 描述需要遵循以下规则:
@Entry 装饰的自定义组件,其 build()函数下的根节点唯一且必要,且必须为容器组件,其中 ForEach 禁止作为根节点。
@Component 装饰的自定义组件,其 build()函数下的根节点唯一且必要,可以为非容器组件,其中 ForEach 禁止作为根节点。
@Entry
@Component
struct MyComponent {
build() {
// 根节点唯一且必要,必须为容器组件
Row() {
ChildComponent()
}
}
}
@Component
struct ChildComponent {
build() {
// 根节点唯一且必要,可为非容器组件
Image('test.jpg')
}
}
不允许声明本地变量
build() {
// 反例:不允许声明本地变量
let a: number = 1;
}
不允许 console
不允许在 UI 描述里直接使用 console.info,但允许在方法或者函数里使用,反例如下。
build() {
// 反例:不允许console.info
console.info('print debug log');
}
不允许创建本地的作用域,反例如下。
build() {
// 反例:不允许本地作用域
{
...
}
}
不允许调用没有用@Builder 装饰的方法
@Component
struct ParentComponent {
doSomeCalculations() {
}
calcTextValue(): string {
return 'Hello World';
}
@Builder doSomeRender() {
Text(`Hello World`)
}
build() {
Column() {
// 反例:不能调用没有用@Builder装饰的方法
this.doSomeCalculations();
// 正例:可以调用
this.doSomeRender();
// 正例:参数可以为调用TS方法的返回值
Text(this.calcTextValue())
}
}
}
不允许使用 switch 语法,如果需要使用条件判断,请使用 if。示例如下
build() {
Column() {
// 反例:不允许使用switch语法
switch (expression) {
case 1:
Text('...')
break;
case 2:
Image('...')
break;
default:
Text('...')
break;
}
// 正例:使用if
if(expression == 1) {
Text('...')
} else if(expression == 2) {
Image('...')
} else {
Text('...')
}
}
}
不允许使用表达式,反例如下
build() {
Column() {
// 反例:不允许使用表达式
(this.aVar > 10) ? Text('...') : Image('...')
}
}
不允许直接改变状态变量
反例如下:
@Component
struct CompA {
@State col1: Color = Color.Yellow;
@State col2: Color = Color.Green;
@State count: number = 1;
build() {
Column() {
// 应避免直接在Text组件内改变count的值
Text(`${this.count++}`)
.width(50)
.height(50)
.fontColor(this.col1)
.onClick(() => {
this.col2 = Color.Red;
})
Button("change col1").onClick(() =>{
this.col1 = Color.Pink;
})
}
.backgroundColor(this.col2)
}
}
自定义组件通用样式
自定义组件通过“.”链式调用的形式设置通用样式。
@Component
struct MyComponent2 {
build() {
Button(`Hello World`)
}
}
@Entry
@Component
struct MyComponent {
build() {
Row() {
MyComponent2()
.width(200)
.height(300)
.backgroundColor(Color.Red)
}
}
}