当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1.4K Star 7.4K Fork 8.2K

OpenHarmony/docs
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
docker
en
zh-cn
application-dev
ability
application-event-logging
connectivity
database
media
public_sys-resources
quick-start
reference
ui
figures
public_sys-resources
Readme-CN.md
arkui-overview.md
js-framework-file.md
js-framework-js-file.md
js-framework-js-tag.md
js-framework-lifecycle.md
js-framework-multiple-languages.md
js-framework-resource-restriction.md
js-framework-syntax-css.md
js-framework-syntax-hml.md
js-framework-syntax-js.md
js-framework-syntax.md
js-framework.md
ts-a-deep-dive-into-component.md
ts-application-states-apis-environment.md
ts-application-states-apis-persistentstorage.md
ts-application-states-appstorage.md
ts-application-states-storagelink-storageprop.md
ts-attribution-configuration.md
ts-child-component-configuration.md
ts-component-based-builder.md
ts-component-based-component.md
ts-component-based-customdialog.md
ts-component-based-entry.md
ts-component-based-extend.md
ts-component-based-preview.md
ts-component-based.md
ts-component-creation-re-initialization.md
ts-component-states-link.md
ts-component-states-prop.md
ts-component-states-state.md
ts-configuration-with-mandatory-parameters.md
ts-custom-component-initialization.md
ts-custom-component-lifecycle-callbacks.md
ts-declarative-syntax.md
ts-declarative-ui-description-specifications.md
ts-event-configuration.md
ts-framework-directory.md
ts-framework-file-access-rules.md
ts-framework-file.md
ts-framework-js-tag.md
ts-framework.md
ts-function-build.md
ts-general-ui-concepts.md
ts-general-ui-description-specifications.md
ts-instantiating-a-struct-without-new-keyword.md
ts-managing-application-states-apis.md
ts-managing-application-states.md
ts-managing-component-states.md
ts-managing-other-states.md
ts-media-resource-type.md
ts-other-states-consume-provide.md
ts-other-states-observed-objectlink.md
ts-other-states-watch.md
ts-parameterless-configuration.md
ts-pixel-units.md
ts-rending-control-syntax-foreach.md
ts-rending-control-syntax-if-else.md
ts-rending-control-syntax-lazyforeach.md
ts-rending-control-syntax.md
ts-resource-access.md
ts-restrictions-for-generators.md
ts-syntactic-sugar-chaining.md
ts-syntactic-sugar-decorator.md
ts-syntactic-sugar-struct.md
ts-syntactic-sugar.md
ts-syntax-intro.md
ts-types.md
ts-ui-state-management.md
ts-ui-state-mgmt-concepts.md
ts-using-a-separate-line-for-new-component.md
ui-arkui-js.md
ui-arkui-ts.md
ui-arkui.md
ui-js-animate-attribute-style.md
ui-js-animate-background-position-style.md
ui-js-animate-component.md
ui-js-animate-css.md
ui-js-animate-dynamic-effects.md
ui-js-animate-frame.md
ui-js-animate-interpolator.md
ui-js-animate-javascript.md
ui-js-animate-transform.md
ui-js-animate.md
ui-js-building-ui-animation.md
ui-js-building-ui-component.md
ui-js-building-ui-event.md
ui-js-building-ui-interactions.md
ui-js-building-ui-layout-comment.md
ui-js-building-ui-layout-external-container.md
ui-js-building-ui-layout-image.md
ui-js-building-ui-layout-intro.md
ui-js-building-ui-layout-text.md
ui-js-building-ui-layout.md
ui-js-building-ui-routes.md
ui-js-building-ui.md
ui-js-common-components.md
ui-js-component-tabs.md
ui-js-components-button.md
ui-js-components-dialog.md
ui-js-components-form.md
ui-js-components-images.md
ui-js-components-input.md
ui-js-components-list.md
ui-js-components-picker.md
ui-js-components-stepper.md
ui-js-components-text.md
ui-js-custom-components.md
ui-js-overview.md
ui-ts-building-category-grid-layout.md
ui-ts-building-category-list-layout.md
ui-ts-building-data-model.md
ui-ts-components.md
ui-ts-creating-project.md
ui-ts-creating-simple-page.md
ui-ts-experiencing-declarative-ui.md
ui-ts-overview.md
ui-ts-page-layout-connections.md
ui-ts-page-redirection-data-transmission.md
usb
Readme-CN.md
application-dev-guide.md
compatibility
contribute
design
device-dev
figures
readme
release-notes
resources
OpenHarmony-Overview_zh.md
readme.md
.gitattributes
.gitignore
DCO.txt
LICENSE
OAT.xml
README.md
README_zh.md
克隆/下载
ts-component-based-component.md 2.68 KB
一键复制 编辑 原始数据 按行查看 历史
zengyawen 提交于 4年前 . update docs

@Component

@Component 装饰的struct表示该结构体具有组件化能力,能够成为一个独立的组件,这种类型的组件也称为自定义组件。

该组件可以组合其他组件,它通过实现build方法来描述UI结构,其必须符合Builder的接口约束,该接口定义如下:

interface Builder {
    build: () => void
}

用户定义的组件具有以下特点:

  • 可组合:允许开发人员组合使用内置组件和其他组件,以及公共属性和方法;

  • 可重用:可以被其他组件重用,并作为不同的实例在不同的父组件或容器中使用;

  • 有生命周期:生命周期的回调方法可以在组件中配置,用于业务逻辑处理;

  • 数据驱动更新:可以由状态数据驱动,实现UI自动更新。

组件生命周期主要包括aboutToAppearaboutToDisappear回调函数,有关规范,请参见“组件生命周期回调函数”章节。

icon-note.gif 说明:

  • 组件必须遵循上述Builder接口约束,其他组件在内部的build方法中以声明式方式进行组合,在组件的第一次创建和更新场景中都会调用build方法。

  • 组件禁止自定义构造函数。

示例

如下代码定义了MyComponent组件**:**

@Component
struct MyComponent {
    build() {
        Column() {
            Text('my component')
                .fontColor(Color.Red)
        }.alignItems(HorizontalAlign.Center) // center align Text inside Column
    }
}

MyComponentbuild方法会在初始渲染时执行,此外,当组件中的状态发生变化时,build方法将再次执行。

以下代码使用了MyComponent组件:

@Component
struct ParentComponent {
    build() {
        Column() {
            MyComponent()
            Text('we use component')
                .fontSize(20)
        }
    }
}

可以多次嵌入MyComponent,并嵌入到不同的组件中进行重用:

@Component
struct ParentComponent {
    build() {
        Row() {
            Column() {
                MyComponent()
                Text('first column')
                    .fontSize(20)
            }
            Column() {
                MyComponent()
                Text('second column')
                    .fontSize(20)
            }
        }
    }

    private aboutToAppear() {
        console.log('ParentComponent: Just created, about to become rendered first time.')
    }

    private aboutToDisappear() {
        console.log('ParentComponent: About to be removed from the UI.')
    }
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
其他
1
https://gitee.com/openharmony/docs.git
git@gitee.com:openharmony/docs.git
openharmony
docs
docs
OpenHarmony-3.1-Beta

搜索帮助