HarmonyOS Array 成员变量属性更改,状态不同步、未能刷新UI

试了各种定义,用链表下标访问时, 组件内的 text 的 title都刷新不了、更不用谈ListItem(里边用的@ObjectLink);

// 换成单个对象捆绑、才会响应, 但是场景是需要用链表。

@Observed 
export class ConversationBean { 
  string title; 
} 
 
@Observed 
class ConversationArray extends Array<ConversationBean> {} 
 
@Observed 
export class ObservedArray<T> extends Array<T> { 
  constructor(args?: T[]) { 
    if (args instanceof Array) { 
      super(...args); 
    } else { 
      super(); 
    } 
  } 
} 
 
@Component 
struct ConversationList { 
  @State conversationList: ObservedArray<ConversationBean> = []//ConversationBean[] = [];//Array<ConversationBean> = [] 
  @State conversationBean: ConversationBean = new ConversationBean(); 
 
  build() { 
    Column() { 
      List() { 
        ForEach(this.conversationList, (item: ConversationBean) => { 
          ListItem() { 
            ConversationListItem({ conversation:  item})//this.conversationBean 
          }.width('100%') 
        }) 
      }.divider({ strokeWidth: 1, startMargin: 15, color: Color.Gray }).backgroundColor(Color.Blue) 
      .onClick(() => {this.test()}) 
      Text(this.conversationList[0].title)//this.conversationBean.title 
    } 
  } 
 
  aboutToAppear(): void { 
    this.conversationList = ConversationViewModel.getConversationList(); 
    this.conversationBean = this.conversationList[0]; 
  } 
  test() { // this.conversationList = ConversationViewModel.getConversationList(); 
    if (this.conversationList.length > 0) { 
      this.conversationList[0].title += '00'; 
    } 
    this.conversationBean.title += 'bean++' 
  } 
}
  • 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.
HarmonyOS
2024-09-02 10:51:37
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以参考简单demo去实现

@Observed 
class ConversationArray extends Array<string> { 
  title?: string; 
  constructor(title:string) { 
    super(); 
    this.title = title; 
  } 
 
 
} 
 
@Entry 
@Component 
struct ConversationList { 
  @State conversationList: Array<ConversationArray> = [new ConversationArray('11')] 
 
  // @State conversationBean: ConversationBean = new ConversationBean(); 
 
  aboutToAppear(): void { 
    // this.conversationList = ConversationViewModel.getConversationList(); 
    // this.conversationBean = this.conversationList[0]; 
 
  } 
 
  build() { 
    Column() { 
      List() { 
        ForEach(this.conversationList, (item: ConversationArray) => { 
          ViewA({item:item,conversationList:this.conversationList}) 
        }) 
      }.divider({ strokeWidth: 1, startMargin: 15, color: Color.Gray }).backgroundColor(Color.Blue) 
      // Text(this.conversationList[0].title)//this.conversationBean.title 
    } 
  } 
} 
 
@Component 
struct ViewA { 
  @ObjectLink item: ConversationArray; 
  conversationList?: Array<ConversationArray>; 
 
  build() { 
    ListItem(){ 
      Text(this.item.title).height(80).width('100%').onClick(() => { 
        if (this.conversationList!.length > 0) { 
          console.log('11111') 
          console.log(this.item.toString()); 
          this.item.title += '00'; 
        }}) 
    } 
  } 
}
  • 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.

参考文档

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

分享
微博
QQ
微信
回复
2024-09-02 17:57:58
相关问题
HarmonyOS 亮度不同步体验不好
867浏览 • 1回复 待解决
HarmonyOS 懒加载列表更改属性UI刷新
779浏览 • 1回复 待解决
解决页面间数据不同步问题
1736浏览 • 1回复 待解决
HarmonyOS 变量状态同步问题
1011浏览 • 1回复 待解决
HarmonyOS 状态变量刷新问题
1604浏览 • 1回复 待解决
状态装饰器 ui刷新的问题
3103浏览 • 1回复 待解决