鸿蒙状态管理-@Builder自定义构建函数

@Builder 将重复使用的UI元素抽象成一个方法 在build方法里调用 使其成为 自定义构建函数

@Entry
@Component
struct BuilderCase {
  build() {
    Column(){
      Row(){
        Text("西游记")
          .fontSize(20)
      }
      .justifyContent(FlexAlign.Center)
      .backgroundColor("#f3f4f5")
      .height(60)
      .borderRadius(8)

      .width("100%")
      .padding({
        left:20,
        right:20
      })
    }
    .padding(20)
  }
}

将其中项 抽离出来

@Entry
@Component
struct BuilderCase {
  @Builder
  getItemBuilder(itemName:string){
    Row(){
      Text(`${itemName}`)
        .fontSize(20)
    }
    .justifyContent(FlexAlign.Center)
    .backgroundColor("#f3f4f5")
    .height(60)
    .borderRadius(8)

    .width("100%")
    .padding({
      left:20,
      right:20
    })
  }

  build() {
    Column({ space:20 }){
      this.getItemBuilder("西游记")
      this.getItemBuilder("水浒传")
      this.getItemBuilder("红楼梦")
      this.getItemBuilder("三国演义")
    }
    .padding(20)
  }
}

同时我们也可以利用循环生成 简化代码

 @State
  list:string[]=[ '西游记','水浒传','红楼梦','三国演义', ]


      ForEach(this.list,(item:string)=>{
        this.getItemBuilder(item)
      })

相关推荐

  1. 构建ArkTS之@Builder装饰器:定义构建函数

    2024-06-13 02:02:03       32 阅读
  2. 鸿蒙定义组件内定义构建函数

    2024-06-13 02:02:03       69 阅读
  3. Android 定义builder对话框

    2024-06-13 02:02:03       62 阅读
  4. react js定义实现状态管理

    2024-06-13 02:02:03       42 阅读
  5. 鸿蒙 - arkTs:状态管理

    2024-06-13 02:02:03       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-13 02:02:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-13 02:02:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-13 02:02:03       82 阅读
  4. Python语言-面向对象

    2024-06-13 02:02:03       91 阅读

热门阅读

  1. mongo数据迁移方法

    2024-06-13 02:02:03       32 阅读
  2. 防护DDoS攻击出现的常见误区

    2024-06-13 02:02:03       26 阅读
  3. moocast(usaco2016年12月金组第1题)

    2024-06-13 02:02:03       29 阅读
  4. c#与汇川plc通信

    2024-06-13 02:02:03       30 阅读
  5. #07【面试问题整理】嵌入式软件工程师

    2024-06-13 02:02:03       29 阅读
  6. leetcode hot100 之 最长公共子序列

    2024-06-13 02:02:03       27 阅读
  7. SSRF-gopher 协议扩展利用:突破网络限制的利器

    2024-06-13 02:02:03       33 阅读
  8. Ant-Design-Vue 动态表头

    2024-06-13 02:02:03       27 阅读
  9. 深入理解ChatGPT工作原理

    2024-06-13 02:02:03       31 阅读
  10. minio

    minio

    2024-06-13 02:02:03      25 阅读
  11. 代码随想录算法训练营第36期DAY52

    2024-06-13 02:02:03       26 阅读
  12. 3D分割之SAGA训练流程解读

    2024-06-13 02:02:03       30 阅读