HarmonyOS鸿蒙Next中build函数内组件内写的多了在加上属性连缀,真的好难阅读

HarmonyOS鸿蒙Next中build函数内组件内写的多了在加上属性连缀,真的好难阅读

interface RecommendDailyType {
  img: string // 图片
  title: string // 简介文字
  type: string // 标题文字
  top: string // 标题背景色
  bottom: string // 简介背景色
}

@Component
  // export 导出,别的组件才可以使用
export struct Recommend {
  swiperList: string[] = [
    'https://s41.ax1x.com/2025/12/24/pZGcq9f.png',
    'https://s41.ax1x.com/2025/12/24/pZGcDB9.png',
    'https://s41.ax1x.com/2025/12/22/pZ82nje.png',
    'https://s41.ax1x.com/2025/12/28/pZYcT41.png',
    'https://s41.ax1x.com/2025/12/24/pZGcvuQ.png',
    'https://s41.ax1x.com/2025/12/24/pZGcvuQ.png'
  ]
  dailyRecommend: RecommendDailyType[] = [
    {
      img: 'https://s41.ax1x.com/2025/12/24/pZGcq9f.png',
      title: '每日推荐 | 今天从《不得不爱》听起 | 私人雷达',
      type: '每日推荐',
      top: '#660000',
      bottom: '#382e2f'
    },
    {
      img: 'https://s41.ax1x.com/2025/12/24/pZGcDB9.png',
      title: '私人漫游 | 今天从《不得不爱》听起 | 私人雷达',
      type: '私人漫游',
      top: '#660000',
      bottom: '#382e2f'
    },
    {
      img: 'https://s41.ax1x.com/2025/12/22/pZ82nje.png',
      title: '华语流行 | 今天从《不得不爱》听起 | 私人雷达',
      type: '华语流行',
      top: '#660000',
      bottom: '#382e2f'
    },
    {
      img: 'https://s41.ax1x.com/2025/12/28/pZYcT41.png',
      title: '每日推荐 | 今天从《不得不爱》听起 | 私人雷达',
      type: '每日推荐',
      top: '#660000',
      bottom: '#382e2f'
    },
    {
      img: 'https://s41.ax1x.com/2025/12/24/pZGcvuQ.png',
      title: '每日推荐 | 今天从《不得不爱》听起 | 私人雷达',
      type: '每日推荐',
      top: '#660000',
      bottom: '#382e2f'
    },
    {
      img: 'https://s41.ax1x.com/2025/12/24/pZGcvuQ.png',
      title: '每日推荐 | 今天从《不得不爱》听起 | 私人雷达',
      type: '每日推荐',
      top: '#660000',
      bottom: '#382e2f'
    },
  ]

  @Builder
  titleBuilder(title: string) {
    Row() {
      Text(title)
        .fontColor('#FFF')
        .fontWeight(700)
        .layoutWeight(1)
      Image($r('app.media.more'))
        .width(22)
        .fillColor('#FFF')
    }
  }

  build() {
    Column({ space: 10 }) {
      Row() {
        Image($r('app.media.search'))
          .width(22)
          .fillColor('#817D83')
        TextInput({ placeholder: '只因你太美🔥' })
          .placeholderColor('#817D83')
          .fontColor('#999')
          .layoutWeight(1)
        Image($r('app.media.sc_code'))
          .width(20)
          .fillColor('#817D83')
      }
      .width('100%')
      .backgroundColor('#2D2B29')
      .border({ radius: 20 })
      .padding({ left: 8, right: 8 })
      .margin({ top: 10 })

      Swiper() {
        ForEach(this.swiperList, (url: string) => {
          Image(url)
            .width('100%')
            .height(150)
            .border({ radius: 10 })
        })
      }
      .autoPlay(true)

      this.titleBuilder('每日推荐')
      List() {
        ForEach(this.dailyRecommend, (item: RecommendDailyType) => {
          ListItem() {
            Column() {
              Text(item.type)
                .width('100%')
                .height(40)
                .backgroundColor(item.top)
                .padding({ left: 5 })
                .fontSize(14)
                .fontColor('#FFF')
              Image(item.img)
                .width('100%')
              Text(item.title)
                .width('100%')
                .backgroundColor(item.bottom)
            }
            .width('40%')
            // .height(200)
            // .backgroundColor(Color.Pink)
            .border({ radius: 10 })
            .margin({ right: 10 })
          }
        })
      }
      .listDirection(Axis.Horizontal)

    }
    .width('100%')
    .height('100%')
    .padding({
      left: 10,
      right: 10,
      top: 5,
      bottom: 5
    })
  }
}

更多关于HarmonyOS鸿蒙Next中build函数内组件内写的多了在加上属性连缀,真的好难阅读的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS Next中,build函数内组件和属性连缀过多会导致代码可读性下降。建议使用组件提取、属性分离、布局拆分等方法优化结构。可将复杂组件拆分为独立函数或自定义组件,使用链式调用时适当换行和缩进,并利用IDE的格式化功能保持代码整洁。

更多关于HarmonyOS鸿蒙Next中build函数内组件内写的多了在加上属性连缀,真的好难阅读的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


这个问题在复杂UI开发中很常见。针对代码可读性,可以尝试以下方法:

  1. 提取子组件:将Swiper和List部分拆分为独立的@Component,例如RecommendSwiperDailyRecommendList组件,减少build函数体积。

  2. 使用@Builder函数:你已经使用了titleBuilder,可以将Row搜索栏、Swiper等复杂部分也提取为单独的[@Builder](/user/Builder)函数。

  3. 链式调用格式化:对属性链式调用进行换行和缩进,保持一致的格式:

    TextInput({ placeholder: '只因你太美🔥' })
      .placeholderColor('#817D83')
      .fontColor('#999')
      .layoutWeight(1)
    
  4. 样式提取:将重复的样式值定义为常量或使用@Styles装饰器集中管理。

  5. 数据与UI分离:考虑将swiperListdailyRecommend数据通过@Prop@Link从父组件传入,使Recommend组件更专注于UI渲染。

这些重构方法能显著提升代码可读性和维护性,特别是在组件复杂度增加时。

回到顶部