鸿蒙 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')
}
}
效果图
最后呢,很多开发朋友不知道需要学习那些鸿蒙技术?鸿蒙开发岗位需要掌握那些核心技术点?为此鸿蒙的开发学习必须要系统性的进行。
而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点
如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。
高清完整版请点击→《鸿蒙NEXT星河版开发学习文档》
针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细资料鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门教学视频,帮助大家在技术的道路上更进一步。
《鸿蒙 (OpenHarmony)开发学习视频》
《鸿蒙生态应用开发V2.0白皮书》
《鸿蒙 (OpenHarmony)开发基础到实战手册》
《鸿蒙开发基础》
《鸿蒙开发进阶》
《鸿蒙开发实战》
获取这份鸿蒙星河版学习资料,请点击→《鸿蒙NEXT星河版开发学习文档》
总结
鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。
并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行!