HarmonyOS NEXT学习——@Builder装饰器自定义构建函数

1.自定义组件内使用

@Component //自定义组件修饰器
struct  Com{
//自定义组件内使用@Build修饰器
  @Builder show(){ 
    Text('Hello World')
  }
  build() {
    this.show() //使用自定义组件内的需要this
  }
}

2全局自定义构建函数

  • 如果不涉及组件状态变化,建议使用全局的自定义构建方法。
//创建全局需要有---- function
@Builder function overBuilder(paramA1: string) {
  Row() {
    Text(paramA1)
  }
}
@Component
struct  Com{
  @Builder shows(label:string){
    Text(label)
  }
  build() {
    Column(){
      this.shows('hello World')
      overBuilder('测试一下下') //使用时不带this
    }
  }
}

你可能感兴趣的:(HarmonyOS,NEXT)