前言
上一篇文章我们介绍了声明式UI描述和自定义组件的基本用法,现在我们系统的看看自定义组件的结构、成员函数、变量等.在《ArkTS应用入门实操》文章里面我们讲解了 ArkTS 的基本代码组成,这里我们看看具体的组件结构。
组件的基本结构
Struct 基本结构
自定义组件基于struct实现
struct + 自定义组件名 + {...}的组合构成自定义组件
- 不能有继承关系
- 对于struct的实例化 可以省略new
Custom components cannot extend or implement other classes and interfaces. <etsLint> 自定义组件无法扩展或实现其他类和接口。<etsLint> ⚠️ 自定义组件名、类名、函数名不能和系统组件名相同
Component 基本结构
@Component装饰器 仅能装饰struct关键字声明
的数据结构
A function or a method can only be decorated by one of the 'Extend, Builder and Styles'. <etsLint> 函数或方法只能由“扩展、生成器和样式”之一修饰。<etsLint>
- struct被@Component装饰后具备组件化的能力
- 需要实现build方法描述UI
This custom component must have a 'build' function. <etsLint> 此自定义组件必须具有“build”功能。<etsLint>
- 一个struct只能被一个@Component装饰
Duplicate decorators for struct are not allowed. <etsLint> 不允许使用重复的 struct 装饰器。<etsLint&