赞
踩
这个ArkTS-自定义component引入只是一个入坑
@Component
export struct compTest{
@State one:string = "Test"
build(){
// Row(){} //build中只能一个Row(横向)或者是Column(纵向)
Column(){
Text(this.one)//Text文本和文本内容
.fontSize(25)//文本字体大小
.fontColor("#f00")//文本字体颜色
.onClick(()=>{//文本字体的点击事件
this.one = 'hello ArkTS' //改变文本
})
}
}
}
创建组件,以上三个是关键信息,意思就是组件就要有@Component装饰器和struct(跟java中的class一个性质)
,在ArkTS
中必须要定义build(){}固定格式
只有被@Entry装饰的组件才可以调用页面的生命周期
。@Entry
修饰的组件)@Entry
@Component
struct Index {
onPageShow(){
console.info("Index页面显示了")
}
build() {
Row() {
//...
}
}
}
@Component
修饰的组件)@Component
export struct compTest{
@State one:string = "Test"
aboutToAppear(){
console.info("自定义组件compTest调用了")
}
build(){
// Row(){} //build中只能一个Row(横向)或者是Column(纵向)
Column(){
//...
}
}
ABuilder( $$ : { paramA1: string, paramB1 : string } );
示例
@Builder function myBuilderTest($$: { param: string }){
Row(){
Text(`${$$.param}`)
}
}
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
build() {
Row() {
Column(){
myBuilderTest({param:this.message})
}
.width('100%')
}
.height('100%')
.backgroundColor('#fff')
}
}
@Builder function myBuilderTest(param: string){
Row(){
Text(`${param}`)
}
}
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
build() {
Row() {
Column(){
myBuilderTest(this.message)
}
.width('100%')
}
.height('100%')
.backgroundColor('#fff')
}
}
也是上面的效果,但是按引用传递参数可以传入对象形式的参数
意思就是自定义组件中添加跳转操作和事件方法会导致引入所有引入该组件的自定义组件都会添加这个功能。为了解决这个问题所以出现了@BuilderParam装饰器
@Builder function myBuilderTest($$: { param: string,param1:string }){
Row(){
Button(`区分${$$.param1}`).onClick(()=>{
console.info(`${$$.param}触发事件了`)
})
}
}
@Entry
@Component
struct Index {
@Builder myBuildTest2(){
Column(){
Button(`额`).onClick(()=>{
console.info(`额触发事件了`)
})
}
};
@BuilderParam Build1: ($$:{ param: string,param1:string }) => void = myBuilderTest;
@BuilderParam Build2: () => void = this.myBuildTest2;
build() {
Row() {
Column(){
myBuilderTest({param:this.message,param1:'这是事件1'})
this.Build2()
}
.width('100%')
}
.height('100%')
.backgroundColor('#fff')
}
}
子组件中@BuilderParam.父组件@Builder使用
@Component
struct Child {
label: string = `Child`
@BuilderParam aBuilder0: () => void;
build() {
Column() {
this.aBuilder0()
}
}
}
@Entry
@Component
struct Parent {
label: string = `Parent`
@Builder componentBuilder() {
Text(`${this.label}`)
}
build() {
Column() {
this.componentBuilder()
Child({ aBuilder0: this.componentBuilder })
}
}
}
俩个组件之间的Builder
@Builder function GlobalBuilder1($$ : {label: string }) {
Text($$.label)
.width(400)
.height(50)
.backgroundColor(Color.Blue)
}
@Component
struct Child {
label: string = 'Child'
// 无参数类,指向的componentBuilder也是无参数类型
@BuilderParam aBuilder0: () => void;
// 有参数类型,指向的GlobalBuilder1也是有参数类型的方法
@BuilderParam aBuilder1: ($$ : { label : string}) => void;
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 })
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。