HarmonyOS 父组件的@state变量更新后子组件不生效

父组件的@state变量更新后子组件对应的@prop变量不生效,未重新build

子组件是一个自定义弹框,需要重新打开弹框才会生效,否则不生效

HarmonyOS
2025-01-09 14:38:44
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

该问题中自定义弹窗@Prop、@ObjectLink不适用此场景。可以使用@Link装饰器。

或者可以使用类封装该变量,参考如下代码:

//@Observed
class ClassA {
  public c: string = '';

  constructor(c: string) {
    this.c = c;
  }
}

// xxx.ets
@CustomDialog
struct CustomDialogExample {

  @Prop str: ClassA
  @Link str1: string
  controller?: CustomDialogController
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }
  build() {
    Column() {
      Text(this.str.c)
        .fontSize(30)
        .height(100)
      Text(this.str1)
        .fontSize(30)
        .height(100)
      Button('点我关闭弹窗')
        .onClick(() => {
          if (this.controller != undefined) {
            this.controller.close()
          }
        })
        .margin(20)
    }
  }
}
@Entry
@Component
struct CustomDialogUser {
  @State str:ClassA = new ClassA('hello world')
  @State str1: string = 'hello world1'
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      str: this.str,
      str1: $str1,
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() }
    }),
    cancel: this.existApp,
    autoCancel: true,
    alignment: DialogAlignment.Center,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    showInSubWindow: true,
    isModal: true,
    customStyle: false,
    cornerRadius: 10,
  })
  aboutToAppear(): void {
    setTimeout(() => {
      this.str.c = '你好,世界'
      this.str1 = '你好,世界'
      console.log('delay 5s');
    }, 5000);
  }

  // 在自定义组件即将析构销毁时将dialogControlle置空
  aboutToDisappear() {
    this.dialogController = null // 将dialogController置空
  }

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  existApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
    Column() {
      Text(this.str.c)
        .height(100).width('100%')
      Text(this.str1)
        .height(100).width('100%')
      Button('click me')
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
分享
微博
QQ
微信
回复
2025-01-09 17:25:22
相关问题
设置组件宽度超出组件
1116浏览 • 1回复 待解决
HarmonyOS @objectLink+@observe组件更新
351浏览 • 1回复 待解决
组件调用组件方法
2050浏览 • 1回复 待解决
用数组变量控制组件属性生效
2151浏览 • 1回复 待解决
HarmonyOS 组件调用组件方法demo
684浏览 • 1回复 待解决
HarmonyOS 组件怎么调用组件方法
1005浏览 • 1回复 待解决
组件调用组件方法
962浏览 • 1回复 待解决
HarmonyOS @State无法更新组件文本
421浏览 • 1回复 待解决
HarmonyOS 组件超过组件范围
609浏览 • 1回复 待解决