HarmonyOS 关于objectLink在数组对象中使用时,修改数据页面不刷新问题

关于objectLink在数组对象中使用时,修改数据页面不刷新,打印数据有变化,希望能再父组件中修改数据时(数组对象)子组件页面能刷新。

HarmonyOS 关于objectLink在数组对象中使用时,修改数据页面不刷新问题-鸿蒙开发者社区

HarmonyOS
2024-08-30 09:28:42
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

需要借助子组件实现

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#objectlink支持联合类型

参考demo

//@Observed @ObjectLink 
@Observed 
class InnerMessageVo { 
 content: string; 
 id: number; 
 readState: boolean; 
 constructor(id: number,content: string,readState: boolean) { 
  this.id = id; 
  this.content = content; 
  this.readState = readState; 
 } 
} 
@Component 
struct ViweA{ 
 @ObjectLink a: InnerMessageVo; 
 build() { 
  Row(){ 
   Text(this.a.content) 
  } 
 } 
} 
@Entry 
@Component 
struct Index2 { 
 @State arr1: InnerMessageVo[] = [new InnerMessageVo(1,"123",true),new InnerMessageVo(2,"123",true),new InnerMessageVo(3,"123",true)] 
 build() { 
  Row() { 
   Column() { 
    List(){ 
     ForEach(this.arr1,(item: InnerMessageVo,index: number)=>{ 
      ListItem(){ 
       ViweA({a:item}) 
       //Text(this.arr1[index].content) 
      } 
     }) 
    } 
    Button('change') 
     .onClick(() => { 
      console.log('123'); 
      this.arr1[1].content = '666' 
     }) 
   } 
   .width('100%') 
  } 
  .height('100%') 
 } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-08-30 17:57:29
相关问题
HarmonyOS使用@ObjectLink 数据刷新
1424浏览 • 2回复 待解决
HarmonyOS 页面数据刷新问题
878浏览 • 1回复 待解决
修改ForEach使用数据对象,UI刷新
2935浏览 • 1回复 待解决
HarmonyOS UI刷新问题
779浏览 • 1回复 待解决
HarmonyOS 页面刷新问题
1095浏览 • 1回复 待解决
HarmonyOS 数组对象数据刷新
838浏览 • 1回复 待解决
HarmonyOS 二维数组刷新问题
1056浏览 • 1回复 待解决
HarmonyOS 数据刷新问题
749浏览 • 1回复 待解决
HarmonyOS 装饰器刷新问题
857浏览 • 1回复 待解决
HarmonyOS 页面跳转刷新问题
1963浏览 • 1回复 待解决
HarmonyOS LazyForEach数据刷新问题
650浏览 • 1回复 待解决
HarmonyOS swiper数据刷新问题
645浏览 • 1回复 待解决
HarmonyOS 状态变量刷新问题
1577浏览 • 1回复 待解决
HarmonyOS使用Refresh下拉刷新问题
1520浏览 • 1回复 待解决