南 筏DevEco Studio
API11, @Builder按照参数引用传值,如果传多个参数,应该怎么写?
@Entry
@Component
struct HollowPage {
@State indicatorTypes: string[] = ['KLINE', 'KDJ', 'BOLL']
@State isShow: boolean = false
build() {
Column() {
ForEach(this.indicatorTypes, (item: string) => {
this.textClick()
})
if (this.isShow) {
Rect().width(100).height(30).backgroundColor('#ff5500')
}
}
}
@Builder
textClick() {
Text('点击展示矩形').width(120).onClick(() => {
this.isShow = !this.isShow
})
}
}接上一篇帖子,builder中的点击事件,断点进不去,这是啥原因导致的,懵!

云朵@Builder中传递的参数需要用一个类封装起来
@Entry
@Component
struct BuilderPage {
@State str: string = "默认的"
@State num: number = 0
build() {
Column() {
Text('点击变化值-按引用传递参数').fontSize(27).onClick(() => {
this.str = '刘小宇'
this.num = 24
})
textClick({ str: this.str, num: this.num })
}.width('100%').margin({ top: 100 })
}
}
// 强制的,要定义一个class里面放要接受参数的字段名
class Tmp {
str: string = ''
num: number = 0
}
@Builder
function
textClick($$: Tmp) {
Row() {
Text(`${$$.str}: ${$$.num}`).fontSize(20).margin({ top: 15 })
}
}
dGuru1、@Builder popupBuilder($$: {param1: <type>, param2: <type>,...}) {
Text(`${$$.param1}`)
}
2、builder: () => { this.popupBuilder({param1: this.state_var, param2: this.state_var2}); }
范小陆这个写法变异报错2024-07-16 07:51 来自北京
dGuru回复范小陆这是NEXT前的老帖了, API 9中语法是如此,NEXT中语法又改了,所以新版IDE下是会报错的。2024-07-16 08:30 来自浙江
范小陆回复dGuru嗯 只能用楼上那种方式了2024-07-16 09:17 来自北京


Tabs 的文档中是通过===来判断应该展示哪个的!
@Entry
@Component
struct TestPage {
@State currentIndex: number = 0
@Builder
tabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
Column() {
Image(this.currentIndex === targetIndex ? selectedImg : normalImg)
.size({ width: 25, height: 25 })
Text(title)
.fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')
}
.width('100%')
.height(50)
.justifyContent(FlexAlign.Center)
}
build() {
Column() {
this.tabBuilder("标题0", 0, $r('app.media.icon_dianzan_1'), $r('app.media.icon_float'))
Button('点击').onClick(() => {
this.currentIndex++
this.currentIndex = this.currentIndex % 2
})
}
}
}
而@Builder中的超过两个参数不能渲染UI,是这种:
@Entry
@Component
struct TestPage {
@State str: string = "默认的"
@State num: number = 0
@Builder
textClick(str: string, num: number) {
Row() {
Text(`${str}: ${num}`).fontSize(20).margin({ top: 15 })
}
}
build() {
Column() {
Text('点击变化值-按值传递参数').fontSize(27).onClick(() => {
this.str = '刘小宇'
this.num = 24
})
this.textClick(this.str, this.num)
}.width('100%').margin({ top: 100 })
}
}
Bob chen@Builder 的文档说了:

但是我在Tabs 的文档中看到了这段:
@Builder tabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
Column() {
Image(this.currentIndex === targetIndex ? selectedImg : normalImg)
.size({ width: 25, height: 25 })
Text(title)
.fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')
}
.width('100%')
.height(50)
.justifyContent(FlexAlign.Center)
}为什么能渲染Ui呢?
南 筏这个倒是给我启发了,我发个代码2024-12-19 05:13 来自辽宁
南 筏这样写可能就方便看:在Builder中显示currentIndex的值 @Entry @Component struct TestPage { @State currentIndex: number = 0 @Builder tabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) { Column() { Text(targetIndex.toString()) Image(this.currentIndex === targetIndex ? selectedImg : normalImg) .size({ width: 25, height: 25 }) Text(title) .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B') } .width('100%') .height(100) .justifyContent(FlexAlign.Center) } build() { Column() { this.tabBuilder("标题0", 0, $r('app.media.icon_dianzan_1'), $r('app.media.icon_float')) Button('点击').onClick(() => { this.currentIndex++ this.currentIndex = this.currentIndex % 2 }) } } }2024-12-19 05:19 来自辽宁
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
HarmonyOS ComponentContent初始化传入builder有多个参数,agrs要怎么传参?(API12+)
HarmonyOS @Builder页面不刷新(API12+)
HarmonyOS 在使用@Builder定义自定义构建函数 (API12+)
HarmonyOS build()和onDidBuild()的调用时机 (API12+)
HarmonyOS BuiderNode的build的参数引用传递 (API12+)
HarmonyOS 如何编写只传一个参数的子组件@component? (API12+)
HarmonyOS Builder函数传参UI不更新问题如何解决?(API12+)
HarmonyOS warpBuilder里面的参数为什么只能支持@Component外面的@Builder,不支持@Component里面的@Builder?(API12+)




































苏公安网备 32011402010933号