ArkTS-自定义组件学习

news/2024/7/21 11:53:10 标签: harmonyos

文章目录

      • 创建自定义组件
      • 页面和自定义组件生命周期
        • 自定义组件和页面的区别
        • 页面生命周期(即被`@Entry`修饰的组件)
        • 组件生命周期(即被`@Component`修饰的组件)
      • @Builder装饰器:自定义构建函数
        • 按引用传递参数
        • 按值传递参数
      • @BuilderParam装饰器:引用@Builder函数

这个ArkTS-自定义component引入只是一个入坑

创建自定义组件

@Component
export  struct compTest{
  @State one:string = "Test"
  build(){
    // Row(){} //build中只能一个Row(横向)或者是Column(纵向)
    Column(){
      Text(this.one)//Text文本和文本内容
        .fontSize(25)//文本字体大小
        .fontColor("#f00")//文本字体颜色
        .onClick(()=>{//文本字体的点击事件
          this.one = 'hello ArkTS' //改变文本
        })
    }
  }
}

在这里插入图片描述
创建组件,以上三个是关键信息,意思就是组件就要有@Component装饰器和struct(跟java中的class一个性质),在ArkTS中必须要定义build(){}固定格式

页面和自定义组件生命周期

自定义组件和页面的区别
  • 自定义组件:@Component装饰的UI单元,可以组合多个系统组件实现UI的复用。
  • 页面:即应用的UI页面。可以由一个或者多个自定义组件组成,@Entry装饰的自定义组件为页面的入口组件,即页面的根节点,一个页面有且仅能有一个@Entry。只有被@Entry装饰的组件才可以调用页面的生命周期
    请添加图片描述
页面生命周期(即被@Entry修饰的组件)
  • onPageShow:页面每次显示时触发
  • onPageHide:页面每次隐藏时触发一次
  • onBackPress:当用户点击返回按钮时触发
@Entry
@Component
struct Index {

  onPageShow(){
    console.info("Index页面显示了")
  }

  build() {
    Row() {
      //...
    }
  }
}

在这里插入图片描述

组件生命周期(即被@Component修饰的组件)
  • aboutToAppear :组件即将出现时回调该接口,具体时机为在创建自定义组件的新实例后,在执行其build()函数之前执行
  • aboutToDisappear:在自定义组件即将析构销毁时执行
@Component
export  struct compTest{
  @State one:string = "Test"

  aboutToAppear(){
    console.info("自定义组件compTest调用了")
  }

  build(){
    // Row(){} //build中只能一个Row(横向)或者是Column(纵向)
    Column(){
      //...
    }
  }

在这里插入图片描述

@Builder装饰器:自定义构建函数

官方@Builder装饰器使用说明

harmonyos.com/cn/docs/documentation/doc-guides-V3/arkts-builder-0000001524176981-V3#section1522464044212" rel="nofollow">按引用传递参数
ABuilder( $$ : { paramA1: string, paramB1 : string } );

示例

@Builder function myBuilderTest($$: { param: string }){
  Row(){
    Text(`${$$.param}`)
  }
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  
  build() {
    Row() {
      Column(){
        myBuilderTest({param:this.message})
      }
      .width('100%')
    }
    .height('100%')
    .backgroundColor('#fff')
  }
}

在这里插入图片描述

harmonyos.com/cn/docs/documentation/doc-guides-V3/arkts-builder-0000001524176981-V3#section163841721135012" rel="nofollow">按值传递参数
@Builder function myBuilderTest(param: string){
  Row(){
    Text(`${param}`)
  }
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  
  build() {
    Row() {
      Column(){
        myBuilderTest(this.message)
      }
      .width('100%')
    }
    .height('100%')
    .backgroundColor('#fff')
  }
}

也是上面的效果,但是按引用传递参数可以传入对象形式的参数

harmonyos.com/cn/docs/documentation/doc-guides-V3/arkts-builderparam-0000001524416541-V3" rel="nofollow">@BuilderParam装饰器:引用@Builder函数

在这里插入图片描述

意思就是自定义组件中添加跳转操作和事件方法会导致引入所有引入该组件的自定义组件都会添加这个功能。为了解决这个问题所以出现了@BuilderParam装饰器

@Builder function myBuilderTest($$: { param: string,param1:string }){
  Row(){
    Button(`区分${$$.param1}`).onClick(()=>{
      console.info(`${$$.param}触发事件了`)
    })
  }
}

@Entry
@Component
struct Index {
  @Builder myBuildTest2(){
    Column(){
      Button(``).onClick(()=>{
        console.info(`额触发事件了`)
      })
    }
  };
  
  @BuilderParam Build1: ($$:{ param: string,param1:string }) => void = myBuilderTest;
  @BuilderParam Build2: () => void = this.myBuildTest2;
  
  build() {
    Row() {
      Column(){
        myBuilderTest({param:this.message,param1:'这是事件1'})
        this.Build2()
      }
      .width('100%')
    }
    .height('100%')
    .backgroundColor('#fff')
  }
}

子组件中@BuilderParam.父组件@Builder使用

@Component
struct Child {
  label: string = `Child`
  @BuilderParam aBuilder0: () => void;

  build() {
    Column() {
      this.aBuilder0()
    }
  }
}

@Entry
@Component
struct Parent {
  label: string = `Parent`

  @Builder componentBuilder() {
    Text(`${this.label}`)
  }

  build() {
    Column() {
      this.componentBuilder()
      Child({ aBuilder0: this.componentBuilder })
    }
  }
}

俩个组件之间的Builder

@Builder function GlobalBuilder1($$ : {label: string }) {
  Text($$.label)
    .width(400)
    .height(50)
    .backgroundColor(Color.Blue)
}

@Component
struct Child {
  label: string = 'Child'
  // 无参数类,指向的componentBuilder也是无参数类型
  @BuilderParam aBuilder0: () => void;
  // 有参数类型,指向的GlobalBuilder1也是有参数类型的方法
  @BuilderParam aBuilder1: ($$ : { label : string}) => void;

  build() {
    Column() {
      this.aBuilder0()
      this.aBuilder1({label: 'global Builder label' } )
    }
  }
}

@Entry
@Component
struct Parent {
  label: string = 'Parent'

  @Builder componentBuilder() {
    Text(`${this.label}`)
  }

  build() {
    Column() {
      this.componentBuilder()
      Child({ aBuilder0: this.componentBuilder, aBuilder1: GlobalBuilder1 })
    }
  }
}

http://www.niftyadmin.cn/n/5231111.html

相关文章

微信群发怎么选择标签群发?

微信好友给某标签分组的好友群发消息操作步骤如下: 1、打开微信,点击【我】里面的【设置】选项。 2、选择设置选项中的【通用】功能设置。 3、选择通用设置选项中的【功能】设置。 4、选择功能选项中的【群发助手】。 5、在群发助手界面&#xff0c…

【Java】使用IntelliJ IDEA搭建SSM(MyBatis-Plus)框架并连接MySQL数据库

步骤 0 准备工作1 创建Maven项目2 配置Maven依赖3 配置数据源4 项目结构5 创建实体类6 创建数据访问层7 创建服务层8 创建Controller层9 启动项目10 使用Postman测试接口 0 准备工作 下载并安装 IntelliJ IDEA下载并安装 MySQL 数据库下载并安装Postman测试工具使用 Navicat 创…

全系降3万,一把干到底,极越「智取」特斯拉

作者|德新 编辑|王博 11月30日,极越01官宣全系降价3万。 这意味着21.99万起步的极越01 Max,成为这个市场上入门门槛最低的带有城市智能驾驶辅助功能的车型。 要知道这是一台比Model Y大了一圈,全系配置了高阶智驾硬件,全系配高…

【android开发-02】android中OptionMenu用法介绍

Option Menu 是 Android 中一种常用的菜单方式,它通常用于在屏幕的顶部显示一个下拉菜单,提供一些常用的功能或操作选项。菜单里的菜单项默认不会显示出来,需要点击右上角三个点的菜单按钮。以下是一些使用 Option Menu 的基本步骤&#xff1…

Redis的基本数据类型及常用命令

Redis通用命令 KEYS命令用于查找所有匹配给定模式 pattern 的 key 。 生产环境不建议使用KEYS命令,会影响效率。 匹配规则: h?llo 匹配 hello, hallo 和 hxlloh*llo 匹配 hllo 和 heeeelloh[ae]llo 匹配 hello and hallo, 不匹配 hilloh[^e]llo 匹配 …

crui_lvgl 一个LVGL的DSL辅助工具的设想

设想 Target以LVGL为目标,语法以CSS为Reference。 CSS 规范 略 CSS规范最强大的属于CSS自身的属性很多,可以通过class和伪属性选择器对UI进行直接控制。 QML规范 ApplicationWindow {visible: truewidth: Constants.widthheight: Constants.height…

四川枢震栩电商:抖店的商品标题怎么设置?

在抖店平台上,商品标题是吸引顾客点击和购买的重要因素之一。一个好的商品标题能够吸引顾客的注意,准确传达商品的特点和卖点。那么,如何设置抖店的商品标题呢? 一、抖店的商品标题怎么设置? 首先,简洁明了…

P4 链表的节点数统计与链表数据查找替换

目录 前言 01 链表的节点数统计 02 链表数据查找替换 2.1 残疾的数据查找 2.2 数据查找优化 前言 🎬 个人主页:ChenPi 🐻推荐专栏1: 《C 》✨✨✨ 🔥 推荐专栏2: 《 Linux C应用编程(概念类)》✨…