HarmonyOS @ObjectLink 装饰器装饰的变量成员子组件中无法感知更新

代码如下

@Entry 
@Component 
struct Index { 
  @State uiState: ObservedA = new ObservedA() 
 
  build() { 
    Row() { 
      Column() { 
        ComponentA({ 
          uiState: this.uiState 
        }) 
 
        Button('addCount') 
          .onClick(() => { 
            this.uiState.ob.b++ 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
 
@Component 
struct ComponentA { 
  @ObjectLink uiState: ObservedA 
 
  build() { 
    ComponentB({ 
      uiState: this.uiState.ob 
    }) 
  } 
} 
 
@Component 
struct ComponentB { 
  @ObjectLink uiState: ObservedB 
 
  build() { 
    ComponentC({ 
      content: this.uiState.b 
    }) 
  } 
} 
 
@Component 
struct ComponentC { 
  @State content: number = 0 
 
  build() { 
    Text(`${this.content}`) 
  } 
} 
 
@Observed 
class ObservedA { 
  a: number = 0 
  ob: ObservedB = new ObservedB() 
} 
 
@Observed 
class ObservedB { 
  b: number = 0 
}
  • 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.

点击按钮更新字段 b,组件 C 中无法感知到更新,如何处理实现该功能

HarmonyOS
2024-08-04 14:35:28
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
e_leaner

参考如下demo:

@Entry 
@Component 
struct Index { 
  @State uiState: ObservedA = new ObservedA() 
 
  build() { 
    Row() { 
      Column() { 
        ComponentA({ 
          uiState: this.uiState 
        }) 
        Button('addCount') 
          .onClick(() => { 
          this.uiState.ob.b++ 
        }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
 
  } 
} 
 
 
@Component 
struct ComponentA { 
  @ObjectLink uiState: ObservedA 
 
 
  build() { 
    ComponentB({ 
      uiState: this.uiState.ob 
    }) 
  } 
} 
 
 
@Component 
struct ComponentB { 
  @ObjectLink uiState: ObservedB 
 
 
  build() { 
    // ComponentC({ 
    //   content: this.uiState.b 
    // }) 
    Text(this.uiState.b.toString()) 
  } 
} 
 
 
// @Component 
// struct ComponentC { 
//   @State content: number = 0 
// 
// 
//   build() { 
//     Text(this.content.toString()) 
//   } 
// } 
 
 
@Observed 
class ObservedA { 
  a: number = 0 
  ob: ObservedB = new ObservedB() 
} 
 
 
@Observed 
class ObservedB { 
  b: number = 0 
}
  • 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.

主要原因是Component C中的@State content: number = 0中@State无法实现父子传值,需要使用@Prop或@Link支持父子间传值的装饰器才可实现,Observed使用可参考如下链接

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-0000001774279618-V5

分享
微博
QQ
微信
回复
2024-08-05 12:09:12
相关问题
HarmonyOS 关于组件装饰问题
973浏览 • 1回复 待解决
HarmonyOS 装饰问题class
844浏览 • 1回复 待解决
HarmonyOS @Concurrent装饰报错
929浏览 • 1回复 待解决
装饰 @Styles 和 @Extend
1411浏览 • 1回复 待解决
HarmonyOS @Observed装饰问题咨询
927浏览 • 1回复 待解决