当前位置: 首页 > article >正文

鸿蒙NEXT开发-自定义构建函数

 注意:博主有个鸿蒙专栏,里面从上到下有关于鸿蒙next的教学文档,大家感兴趣可以学习下

如果大家觉得博主文章写的好的话,可以点下关注,博主会一直更新鸿蒙next相关知识

目录

1. 构建函数-@Builder

1.1 基本介绍

1.2 用法

1.2.1 局部定义代码示例

1.2.2 全局定义代码示例

2. 构建函数-传参传递

2.1 基本介绍

2.2 按值传递参数

2.3 按引用传递参数

3. 构建函数-@BuilderParam

3.1 基本介绍

3.2 尾随闭包初始化组件

3.3 参数传递初始化组件


1. 构建函数-@Builder

1.1 基本介绍

如果你不想在直接用组件,ArkUI还提供了一种更轻量的UI元素复用机制 @Builder,可以将重复使用的UI元素抽象成一个方法,在 build 方法里调用。称之为自定义构建函数

1.2 用法

局部定义:使用@Builder修饰符修饰函数,在组件中使用

@Builder MyBuilderFunction() {}

使用方法:this.MyBuilderFunction()

全局定义:使用@Builder修饰符修饰函数,记得要加上function

@Builder function MyGlobalBuilderFunction() { ... }

使用方法:MyGlobalBuilderFunction()

假设你有N个这样的单个元素,但是重复的去写会浪费大量的代码,丧失代码的可读性,此时我们就可以使用builder构建函数,如果不涉及组件状态变化,建议使用全局的自定义构建方法。

1.2.1 局部定义代码示例

@Entry
  @Component
  struct Index {
    @State
    list: string[] = ["A", "B","C", "D", "E", "F"]

    @Builder
    getItemBuilder (itemName: string) {
      Row() {
        Text(`${itemName}. 选项`)
      }
      .height(60)
        .backgroundColor("#ffe0dede")
        .borderRadius(8)
        .width("100%")
        .padding({
          left: 20,
          right: 20
        })
    }

    build() {
      Column({ space: 10 }) {
        ForEach(this.list, (item: string) => {
          this.getItemBuilder(item)
        })
      }
      .padding(20)
    }
  }

1.2.2 全局定义代码示例

@Entry
  @Component
  struct Index {
    @State
    list: string[] = ["A", "B","C", "D", "E", "F"]

    build() {
      Column({ space: 10 }) {
        ForEach(this.list, (item: string) => {
          getItemBuilder(item)
        })
      }
      .padding(20)
    }
  }


@Builder
  function getItemBuilder (itemName: string) {
    Row() {
      Text(`${itemName}. 选项`)
    }
    .height(60)
      .backgroundColor("#ffe0dede")
      .borderRadius(8)
      .width("100%")
      .padding({
        left: 20,
        right: 20
      })
  }

2. 构建函数-传参传递

2.1 基本介绍

自定义构建函数的参数传递有按值传递和按引用传递两种,均需遵守以下规则:

  • 参数的类型必须与参数声明的类型一致,不允许undefined、null和返回undefined、null的表达式。
  • 在自定义构建函数内部,不允许改变参数值。如果需要改变参数值,且同步回调用点,建议使用@Link。
  • @Builder内UI语法遵循UI语法规则。

2.2 按值传递参数

调用@Builder装饰的函数默认按值传递。当传递的参数为状态变量时,状态变量的改变不会引起@Builder方法内的UI刷新。所以当使用状态变量的时候,推荐使用按引用传递

@Entry
  @Component
  struct Index {

    @State name:string='张三'
    @State age:number=18

    build() {
      Column() {
        Text(this.name)
          .fontSize(40)
          .width(200)
          .height(100)
          .backgroundColor(Color.Blue)

        getTextAge(this.age)

      }
      .width('100%')
        .height('100%')
    }
  }

@Builder
  function getTextAge(age:number){
    Text(age.toString())
      .fontSize(40)
      .width(200)
      .height(100)
      .backgroundColor(Color.Yellow)
  }
@Entry
  @Component
  struct Index {

    @State name:string='张三'
    @State age:number=18

    build() {
      Column() {
        Text(this.name)
          .fontSize(40)
          .width(200)
          .height(100)
          .backgroundColor(Color.Blue)
          .onClick(()=>{
            this.age=30
          })

        getTextAge(this.age)

      }
      .width('100%')
        .height('100%')
    }
  }

@Builder
  function getTextAge(age:number){
    Text(age.toString())
      .fontSize(40)
      .width(200)
      .height(100)
      .backgroundColor(Color.Yellow)
  }

2.3 按引用传递参数

按引用传递参数时,传递的参数可为状态变量,且状态变量的改变会引起@Builder方法内的UI刷新。

@Entry
  @Component
  struct Index {

    @State name:string='张三'
    @State age:number=18

    build() {
      Column() {
        Text(this.name)
          .fontSize(40)
          .width(200)
          .height(100)
          .backgroundColor(Color.Blue)
          .onClick(()=>{
            this.age=30
          })

        getTextAge({age:this.age})

      }
      .width('100%')
        .height('100%')
    }
  }

@Builder
  function getTextAge(params:Test){
    Text(params.age.toString())
      .fontSize(40)
      .width(200)
      .height(100)
      .backgroundColor(Color.Yellow)
  }

class Test{
  age:number=0
}

3. 构建函数-@BuilderParam

3.1 基本介绍

@BuilderParam:是一个装饰器,用于声明任意UI描述的一个元素,类似于vue里的 slot 占位符。利用@BuilderParam构建函数,可以让自定义组件允许外部传递UI

@BuilderParam装饰的方法只能被自定义构建函数(@Builder装饰的方法)初始化。

3.2 尾随闭包初始化组件

@Entry
  @Component
  struct Index {

    build() {
      Column() {
        Test(){
            Button('点击按钮'	)
        }
      }
      .width('100%')
        .height('100%')
    }
  }

@Component
  struct Test{
    // 1、定义BuilderParam接受外部传入的ui,并设置默认值
    @BuilderParam contentBuilder:()=>void=this.defaultBuilder

    // 默认的Builder
    @Builder
    defaultBuilder(){
      Text('默认内容')
    }
    build() {
      Column(){
        // 2、使用@BuilderParam装饰的成员变量
        this.contentBuilder()
      }
    }
  }

3.3 参数传递初始化组件

多个@BuilderParam参数

子组件有多个BuilderParam,必须通过参数的方式来传入

@Entry
  @Component
  struct Index {
    @Builder
    test1() {
      Button('点击1')
    }

    @Builder
    test2() {
      Button('点击2')
    }

    build() {
      Column() {
        Test({
          contentBuilder:  () => {this.test1()},
          textBuilder: () => {this.test2()}
        })
      }
      .width('100%')
        .height('100%')
    }
  }

@Component
  struct Test {
    // 定义BuilderParam接受外部传入的ui,并设置默认值
    @BuilderParam contentBuilder: () => void = this.cDefaultBuilder
    @BuilderParam textBuilder: () => void = this.tDefaultBuilder

    @Builder
    cDefaultBuilder() {
      Text('默认内容1')
    }

    @Builder
    tDefaultBuilder() {
      Text('默认内容2')
    }

    build() {
      Column() {
        // 使用@BuilderParam装饰的成员变量
        this.contentBuilder()
        this.textBuilder()
      }
    }
  }


http://www.kler.cn/a/549757.html

相关文章:

  • mac docker镜像加速正确配置方式
  • rabbitmq五种模式的总结——附java-se实现(详细)
  • Vue 自动配置表单 el-switch等不常用组件覆盖默认值问题
  • Versal - 基础5(裸机开发 AIE-ML+Vitis2024.2界面aie report介绍)
  • 基于Python实现的缓存淘汰替换策略算法,该算法将缓存分区
  • 网络安全-攻击流程-应用层
  • Java每日精进·45天挑战·Day17
  • 【第3章:卷积神经网络(CNN)——3.1 CNN的基本结构与工作原理】
  • 大语言模型推理中的显存优化 有哪些
  • 如何利用Vuex的插件来记录和追踪状态变化?
  • Linux下tomcat实现进程守护
  • PostgreSQL如何关闭自动commit
  • PHP框架入门指南:从零构建现代Web应用
  • GO切片slice详细解析
  • (PC+WAP) PbootCMS中小学教育培训机构网站模板 – 绿色小学学校网站源码下载
  • 【第12章:深度学习与伦理、隐私—12.4 深度学习与伦理、隐私领域的未来挑战与应对策略】
  • DeepSeek 服务器繁忙的全面解决方案
  • 铁塔电单车协议对接电单车TCP json协议对接成熟充电桩系统搭建低速充电桩TCP 接口规范
  • 【第14章:神经符号集成与可解释AI—14.2 可解释AI技术:LIME、SHAP等的实现与应用案例】
  • 深入解析:如何利用 Python 爬虫获取淘宝/天猫 SKU 详细信息