HarmonyOS 鸿蒙 ArkTs 自定义组件插槽

HarmonyOS 鸿蒙 arkts中想要自定义组件中要传入其他组件的时候就可以使用自定义组件插槽。

可以在ArkUI引入了@BuilderParam装饰器。@BuilderParam用来装饰指向@Builder方法的变量,开发者可在初始化自定义组件时对此属性进行赋值,为自定义组件增加特定的功能。该装饰器用于声明任意UI描述的一个元素,类似slot占位符。
样例

插槽的使用

自定义组件

// 子组件
@Component
  export struct Card {
    @Builder
    slot() {
      Text('暂无数据').margin(30)
    };

    private title: ResourceStr
    @BuilderParam component: () => void = this.slot;

    build() {
      Column() {
        Row() {
          Text(this.title)
            .fontColor('#333333')
            .fontSize(18)
            .fontWeight(700)
            .lineHeight(26)
        }.justifyContent(FlexAlign.Start).width('100%')

        this.component()
      }
      .backgroundColor(Color.White)
        .width('100%')
        .padding({ left: 16, right: 16, top: 20, bottom: 20 })
        .borderRadius(12)
        .margin({ bottom: 12 })
    }
  }

外部调用

@Builder
function overBuilder() {
  Text('外部组件').margin(30)
}

// 父布局调用:
@Preview
@Component
@Entry
export struct Index {
  @Builder
  componentBuilder() {
    Text('内部组件').margin(30)
  }

  build() {
    Column() {
      Card({ title: '默认插槽' })
      Card({ title: '使用内部的组件插槽', component: this.componentBuilder })
      Card({ title: '使用外部的组件插槽', component: overBuilder })
      Card({ title: '直接传值' }) {
        Text('直接传值').margin(30)
      }

    }.height('100%').padding({ left: 16, right: 16, top: 20, bottom: 20 }).backgroundColor('#f0f3f6')
  }
}

效果图

以上的例子,都是单个插槽,那如果想使用多个插槽呢?

多个插槽使用

子组件有多个BuilderParam,必须通过参数的方式来传入

自定义组件


// 子组件
@Component
export struct Card {
  @Builder
  slot() {
    Text('暂无数据').margin(30)
  };

  @Builder
  defaultRightSlot() {
    Text('详情')
  };

  private title: ResourceStr
  @BuilderParam component: () => void = this.slot;
  @BuilderParam rightSlot: () => void = this.defaultRightSlot;

  build() {
    Column() {
      Row() {
        Text(this.title)
          .fontColor('#333333')
          .fontSize(18)
          .fontWeight(700)
          .lineHeight(26)
        this.rightSlot()

      }.justifyContent(FlexAlign.SpaceBetween).width('100%')

      this.component()
    }
    .backgroundColor(Color.White)
    .width('100%')
    .padding({ left: 16, right: 16, top: 20, bottom: 20 })
    .borderRadius(12)
    .margin({ bottom: 12 })
  }
}

外部调用

@Builder
function overBuilder() {
  Text('外部组件').margin(30)
}

// 父布局调用:
@Preview
@Component
@Entry
export struct Index {
  @Builder
  componentBuilder() {
    Text('内部组件').margin(30)
  }

  @Builder
  rightBuilder() {
    Text('右边的插槽').margin(30)
  }

  build() {
    Column() {
      Card({ title: '默认插槽' })
      Card({ title: '通过参数传递', component: this.componentBuilder, rightSlot: this.rightBuilder })
      // Card({ title: '会报错' }) {
      //   Text('直接传值').margin(30)
      // }
    }.height('100%').padding({ left: 16, right: 16, top: 20, bottom: 20 }).backgroundColor('#f0f3f6')
  }
}

效果图

插槽的实现方式有很多,具体可以参考下官方文档@BuilderParam装饰器:引用@Builder函数

  • 12
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值