HarmonyOS @BuilderParam 中变量变化,如何触发页面刷新

情况大致如下面代码的描述,当ContainerPage中的方法increaseParam()触发时,希望可以刷新SubComponent展示的内容。有什么优雅的办法吗 ?

// SubComponent.ets
@Component
export struct SubComponent {
  @BuilderParam public content?: () => void
  build() {
    row() {
      this.content()
    }
  }

}


// ContainerPage.ets
@Entry
@Component
export struct ContainerPage {
  @State param: number = 0

  build() {
    row() {
      SubComponent({
        content: () => {
          Text(`${this.param}`)
        }
      })
    }
  }

  increaseParam() {
    this.param += 1
  }

}
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

可参考如下简易demo:

@Entry
@Component
struct buildParam {
  @State message: string = 'It is Page';
  isCurrenshowFoldMode:boolean = true;
  @State buttonText: string = '按钮Parent';

  aboutToAppear(): void {
    this.isCurrenshowFoldMode = true;
    this.buttonText = '按钮';
  }
  @Builder
  floorBuilder(){
    Column(){
      Button(this.buttonText+'666')
        .onClick(()=>{
          this.buttonText = this.isCurrenshowFoldMode ? '按钮001' : '按钮002'
          this.isCurrenshowFoldMode = !this.isCurrenshowFoldMode
          console.log('zhouyishun:::buttonText='+ this.buttonText)
        })
    }.padding(6).width('100%')
  }

  build() {
    Column() {
      baseFloorView({
        childBuilder: this.floorBuilder
      })
    }
  }
}

@Component
export struct baseFloorView{
  @State buttonText: string = '按钮Child';
  @BuilderParam childBuilder :() => void ;

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

this指向问题参考下面链接说明:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-builderparam-0000001820999553#ZH-CN_TOPIC_0000001811317314__装饰器使用说明

分享
微博
QQ
微信
回复
2天前
相关问题
@BuilderParam 不支持普通class的变量
807浏览 • 1回复 待解决
ArkTS如何监听状态变量变化
1034浏览 • 1回复 待解决
HarmonyOS 如何监听某个变量是否变化
58浏览 • 1回复 待解决
如何订阅系统环境变量变化
438浏览 • 1回复 待解决
Canvas如何触发刷新重复绘制?
998浏览 • 1回复 待解决