ArkTS中自定义组件

本文介绍了如何在ArkTS项目中创建和使用自定义组件,如Hello.ets组件的结构和用法,以及在Second.ets中的应用,强调了组件命名规则和@Component/@Entry装饰器的作用。
摘要由CSDN通过智能技术生成

一、组件位置

一个项目下所有的自定义的组件名不可以重复,无论是否在一个ets文件中

在这里插入图片描述

二、Hello.ets自定义组件

自定义组件

1:组件必须使用@Component装饰
2:@Entry装饰在哪个组件上,页面就展示哪个组件(主组件)
3:被@Entry装饰的主组件,build()中必须有且仅有一个根容器组件,其它自定义组件,build()中有且仅有一个根组件。

@Component
export struct Hello {
    private  title:ResourceStr
     build(){
       Row(){
         Text(this.title)
           .fontSize(30)
           .fontWeight(FontWeight.Bold)
       }
     }

}

三、Second.ets父组件

在这里插入图片描述

// Second.ets
import {Hello} from '../compontent/Hello'
@Entry
@Component
struct Second {
  @State message: string = 'Hi there'
  build() {
    Row() {
      Column() {
        Hello({title:'hello组件'}
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Divider()
      }
      .width('100%')
    }
    .height('100%')
  }
}

链接: https://blog.csdn.net/dfsdcvbhjnj/article/details/134744131?spm=1001.2014.3001.5502

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值