关于openharmony:OpenHarmony装饰指定自定义组件BuilderParam装饰器

2次阅读

共计 3503 个字符,预计需要花费 9 分钟才能阅读完成。

当开发者创立了自定义组件,并想对该组件增加特定性能时,例如在自定义组件中增加一个点击跳转操作。若间接在组件内嵌入事件办法,将会导致所有引入该自定义组件的中央均减少了该性能。为解决此问题,ArkUI 引入了 @BuilderParam 装璜器,@BuilderParam 用来装璜指向 @Builder 办法的变量,开发者可在初始化自定义组件时对此属性进行赋值,为自定义组件减少特定的性能。该装璜器用于申明任意 UI 形容的一个元素,相似 slot 占位符。

阐明:

从 API version 9 开始,该装璜器反对在 ArkTS 卡片中应用。

装璜器应用阐明

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

● 应用所属自定义组件的自定义构建函数或者全局的自定义构建函数,在本地初始化 @BuilderParam。

@Builder function GlobalBuilder0() {}

@Component
struct Child {@Builder doNothingBuilder() {};

  @BuilderParam aBuilder0: () => void = this.doNothingBuilder;
  @BuilderParam aBuilder1: () => void = GlobalBuilder0;
  build(){}
}

● 用父组件自定义构建函数初始化子组件 @BuildParam 装璜的办法。

@Component
struct Child {@Builder componentBuilder() {Text(`Parent builder `)
  }

  @BuilderParam aBuilder0: () => void = this.componentBuilder;

  build() {Column() {this.aBuilder0()
    }
  }
}

@Entry
@Component
struct Parent {@Builder componentBuilder() {Text(`Parent builder `)
  }

  build() {Column() {Child({ aBuilder0: this.componentBuilder})
    }
  }
}

● 需注意 this 指向正确。

以下示例中,Parent 组件在调用 this.componentBuilder()时,this.label 指向其所属组件,即“Parent”。@Builder componentBuilder()传给子组件 @BuilderParam aBuilder0,在 Child 组件中调用 this.aBuilder0()时,this.label 指向在 Child 的 label,即“Child”。对于 @BuilderParam aBuilder1,在将 this.componentBuilder 传给 aBuilder1 时,调用 bind 绑定了 this,因而其 this.label 指向 Parent 的 label。

阐明:

开发者审慎应用 bind 扭转函数调用的上下文,可能会使 this 指向凌乱。

@Component
struct Child {@Builder componentBuilder() {Text(`Child builder `)
  }

  label: string = `Child`
  @BuilderParam aBuilder0: () => void = this.componentBuilder;
  @BuilderParam aBuilder1: () => void = this.componentBuilder;

  build() {Column() {this.aBuilder0()
      this.aBuilder1()}
  }
}

@Entry
@Component
struct Parent {
  label: string = `Parent`

  @Builder componentBuilder() {Text(`${this.label}`)
  }

  build() {Column() {this.componentBuilder()
      Child({aBuilder0: this.componentBuilder, aBuilder1: this.componentBuilder})
    }
  }
}

应用场景

参数初始化组件

@BuilderParam 装璜的办法能够是有参数和无参数的两种模式,需与指向的 @Builder 办法类型匹配。@BuilderParam 装璜的办法类型须要和 @Builder 办法类型统一。

class GlobalBuilderParam {label: string = ""}

@Builder function GlobalBuilder1($$ : GlobalBuilderParam) {Text($$.label)
    .width(400)
    .height(50)
    .backgroundColor(Color.Blue)
}

@Component
struct Child {@Builder componentBuilder() {Text(`Child builder `)
  }

  label: string = 'Child'
  // 无参数类,指向的 componentBuilder 也是无参数类型
  @BuilderParam aBuilder0: () => void = this.componentBuilder;
  // 有参数类型,指向的 GlobalBuilder1 也是有参数类型的办法
  @BuilderParam aBuilder1: ($$ : GlobalBuilderParam) => void = this.componentBuilder;

  build() {Column() {this.aBuilder0()
      this.aBuilder1({label: 'global Builder label'} )
    }
  }
}

@Entry
@Component
struct Parent {
  label: string = 'Parent'

  @Builder componentBuilder() {Text(`${this.label}`)
  }

  build() {Column() {this.componentBuilder()
      Child({aBuilder0: this.componentBuilder, aBuilder1: GlobalBuilder1})
    }
  }
}

尾随闭包初始化组件
在自定义组件中应用 @BuilderParam 装璜的属性时也可通过尾随闭包进行初始化。在初始化自定义组件时,组件后紧跟一个大括号“{}”造成尾随闭包场景。

阐明:

此场景下自定义组件内有且仅有一个应用 @BuilderParam 装璜的属性。

开发者能够将尾随闭包内的内容看做 @Builder 装璜的函数传给 @BuilderParam。示例如下:

// xxx.ets
class CustomContainerParam {header: string = '';}
@Component
struct CustomContainer {@Builder componentCloser() {Text(`Custom closer `)
  }

  @Prop header: string = '';
  @BuilderParam closer: () => void = this.componentCloser;

  build() {Column() {Text(this.header)
        .fontSize(30)
      this.closer()}
  }
}

@Builder function specificParam(label1: string, label2: string) {Column() {Text(label1)
      .fontSize(30)
    Text(label2)
      .fontSize(30)
  }
}

@Entry
@Component
struct CustomContainerUser {
  @State text: string = 'header';
  param: CustomContainerParam = {header: this.text};

  build() {Column() {// 创立 CustomContainer,在创立 CustomContainer 时,通过其后紧跟一个大括号“{}”造成尾随闭包
      // 作为传递给子组件 CustomContainer @BuilderParam closer: () => void 的参数
      CustomContainer(this.param) {Column() {specificParam('testA', 'testB')
        }.backgroundColor(Color.Yellow)
        .onClick(() => {this.text = 'changeHeader';})
      }
    }
  }
}
正文完
 0