Web前端之路ArkTS/ArkUI实战零基础学鸿蒙编程

48、鸿蒙/@Builder装饰器:自定义构建函数

2024-08-09  本文已影响0人  圆梦人生

ArkUI还提供了一种更轻量的UI元素复用机制@Builder,@Builder所装饰的函数遵循build()函数语法规则,开发者可以将重复使用的UI元素抽象成一个方法,在build方法里调用。

装饰器使用说明

自定义组件内自定义构建函数

定义的语法:

@Builder MyBuilderFunction() { ... }

使用方法:

this.MyBuilderFunction()

局部组件案例

@Component
export struct BuilderLayout {
  build() {
    this.myComponent()
  }

  // 自定义局部组件
  @Builder myComponent() {
    Row(){
      Column(){
        Text('首页')
      }.layoutWeight(1)
      Column(){
        Text('新闻')
      }.layoutWeight(1)
      Column(){
        Text('推荐')
      }.layoutWeight(1)
      Column(){
        Text('我的')
      }.layoutWeight(1)
    }.backgroundColor(Color.Gray)
    .height(60)
  }
}

效果

image.png
全局自定义构建函数

定义的语法:

@Builder function MyGlobalBuilderFunction() { ... }

使用方法:

MyGlobalBuilderFunction()

全局组件案例

// 全局组件
@Builder function qjComponent() {
  Row(){
    Column(){
      Text('首页1')
    }.layoutWeight(1)
    Column(){
      Text('新闻1')
    }.layoutWeight(1)
    Column(){
      Text('推荐1')
    }.layoutWeight(1)
    Column(){
      Text('我的1')
    }.layoutWeight(1)
  }.backgroundColor(Color.Orange)
  .height(60)
}

@Component
export struct BuilderLayout {
  build() {
    Column(){
      // 局部
      this.myComponent()
      // 全局
      qjComponent()
    }
  }

  // 自定义局部组件
  @Builder myComponent() {
    Row(){
      Column(){
        Text('首页')
      }.layoutWeight(1)
      Column(){
        Text('新闻')
      }.layoutWeight(1)
      Column(){
        Text('推荐')
      }.layoutWeight(1)
      Column(){
        Text('我的')
      }.layoutWeight(1)
    }.backgroundColor(Color.Gray)
    .height(60)
  }
}

效果

全局组件.png

参数传递规则

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

按引用传递参数

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

// 定义参数类型
class myParams {
  message: string = ''
}

@Component
export struct BuilderLayout {
  //
  @State homeTxt: string = '首页'
  build() {
    Column(){
      // 局部
      this.myComponent({ message: this.homeTxt})
      Button('点击改变首页').onClick((e)=>{
        this.homeTxt = 'Home'
        console.log('btn click', this.homeTxt);
      })
    }
  }

  // 自定义局部组件
  @Builder myComponent(params: myParams) {
    Row(){
      Column(){
        Text(`${params.message}`)
      }.layoutWeight(1)
      Column(){
        Text('新闻')
      }.layoutWeight(1)
      Column(){
        Text('推荐')
      }.layoutWeight(1)
      Column(){
        Text('我的')
      }.layoutWeight(1)
    }.backgroundColor(Color.Gray)
    .height(60)
  }
}
效果
引用参数.png

按引用传递参数时,如果在@Builder方法内调用自定义组件,ArkUI提供$$作为按引用传递参数的范式。

class myParams {
  message: string = ''
}

//
@Component
struct myCompoenent2 {
  // 接受从外部获取参数
  @Prop msg:string = ''
  build() {
    Text(this.msg)
  }
}

@Component
export struct BuilderLayout {
  //
  @State homeTxt: string = '首页'
  build() {
    Column(){
      // 局部
      this.myComponent({ message: this.homeTxt})
      Button('点击改变首页').onClick((e)=>{
        this.homeTxt = 'Home'
        console.log('btn click', this.homeTxt);
      })
      // 全局
      // qjComponent()
    }
  }

  // 自定义局部组件
  @Builder myComponent($$: myParams) {
    Row(){
      Column(){
        Text(`${$$.message}`)
        // 调用另外一个组件
        myCompoenent2({msg: $$.message});

      }.layoutWeight(1)
      Column(){
        Text('新闻')
      }.layoutWeight(1)
      Column(){
        Text('推荐')
      }.layoutWeight(1)
      Column(){
        Text('我的')
      }.layoutWeight(1)
    }.backgroundColor(Color.Gray)
    .height(60)
  }
}
效果
$$值引用.png
按值传递参数

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

@Component
export struct BuilderLayout {
  //
  @State homeTxt: string = '首页'
  build() {
    Column(){
      // 局部
      this.myComponent(this.homeTxt)
      Button('点击不改变首页').onClick((e)=>{
        this.homeTxt = 'Home'
        console.log('btn click', this.homeTxt);
      })
    }
  }

  // 自定义局部组件
  @Builder myComponent(homeTxt: string) {
    Row(){
      Column(){
        Text(`${homeTxt}`)
      }.layoutWeight(1)
      Column(){
        Text('新闻')
      }.layoutWeight(1)
      Column(){
        Text('推荐')
      }.layoutWeight(1)
      Column(){
        Text('我的')
      }.layoutWeight(1)
    }.backgroundColor(Color.Gray)
    .height(60)
  }
}
效果
按值引用.png
上一篇下一篇

猜你喜欢

热点阅读